6 < 7 || 7 >=8 11 > 5 - McLean County Unit District No. 5 · PDF file ·...

8
6 < 7 || 7 >=8 11 > 5 5 + 3 <= 9 && 2 > 3 Table of Contents Date(s) Title/Topic Page #s 3/14 56-57 3.2 The if Statement Set up p. 56 for Pre-Code and p. 57 for Cornell notes 57 3.2 The if Statement 56 Absolute Value Pre-Code you need your computer today

Transcript of 6 < 7 || 7 >=8 11 > 5 - McLean County Unit District No. 5 · PDF file ·...

Page 1: 6 < 7 || 7 >=8 11 > 5 - McLean County Unit District No. 5 · PDF file · 2016-03-13Set up p. 56 for Pre-Code and p. 57 for Cornell notes 57 3.2 The if Statement 56 Absolute Value

6 < 7 || 7 >=8 11 > 5

5 + 3 <= 9 && 2 > 3

Table of ContentsDate(s) Title/Topic Page #s

3/14 56-573.2 The if Statement

Set up p. 56 for Pre-Code and p. 57 for Cornell notes

57

3.2 The if Statement

56

Absolute Value Pre­Code

you need your

computer today

Page 2: 6 < 7 || 7 >=8 11 > 5 - McLean County Unit District No. 5 · PDF file · 2016-03-13Set up p. 56 for Pre-Code and p. 57 for Cornell notes 57 3.2 The if Statement 56 Absolute Value

Understanding TruthTruth is black and white, at least as far as C++ is concerned. You

can represent true and false with their corresponding keywords,

true and false. You can store such a Boolean value with a bool

variable, as you saw in Chapter 1.

Here’s a quick refresher:

bool fact = true, fiction = false;

This code creates two bool variables, fact and fiction. fact is true and

fiction is false. Although the keywords true and false are handy, any

expression or value can be interpreted as true or false, too. Any

non-zero value can be interpreted as true, while 0 can be

interpreted as false.

A common kind of expression interpreted as true or false involves comparing

things. Comparisons are often made by using built-in relational operators.

Table 2.1 lists the operators and a few sample expressions.

Page 3: 6 < 7 || 7 >=8 11 > 5 - McLean County Unit District No. 5 · PDF file · 2016-03-13Set up p. 56 for Pre-Code and p. 57 for Cornell notes 57 3.2 The if Statement 56 Absolute Value

The ! Operator

Page 4: 6 < 7 || 7 >=8 11 > 5 - McLean County Unit District No. 5 · PDF file · 2016-03-13Set up p. 56 for Pre-Code and p. 57 for Cornell notes 57 3.2 The if Statement 56 Absolute Value

Short-Circuit EvaluationShort-circuit evaluation: evaluation of a logical

expression stops as soon as the value of theexpression is known

Example

age = 23;

grade = ‘B’;x = 10;

(age >= 21) || (x == 5)

(grade == 'A') && (x >= 7)

Data Type and Logical (Boolean) Expressions

The data type has logical (Boolean) valuestrue falseCompiler reads true false

If a number is stored to bool that does not equal 0 or 1

true = nonzero and false = zero

true false are reserved word

Examplesbool legalAge = true; //define and assignlegalAge = (age >= 21); // new assignment (update)

Page 5: 6 < 7 || 7 >=8 11 > 5 - McLean County Unit District No. 5 · PDF file · 2016-03-13Set up p. 56 for Pre-Code and p. 57 for Cornell notes 57 3.2 The if Statement 56 Absolute Value

Formatting Logical (Boolean)Expressions: A Precaution

Logical expressions can be unpredictable. Watch yourformatting of comparisons!

The following expression appears to represent acomparison of 0, num, and 10:

0 <= num <= 10

It always evaluates to because 0 <= num evaluates

to either 0 or 1, 0 <= 10 is , and 1 <= 10 isA correct way to write this expression is:

0 <= num && num <= 10

C++ has 3 selections (or branch structures)

if...else

switch

Page 6: 6 < 7 || 7 >=8 11 > 5 - McLean County Unit District No. 5 · PDF file · 2016-03-13Set up p. 56 for Pre-Code and p. 57 for Cornell notes 57 3.2 The if Statement 56 Absolute Value

Using the if StatementOkay, it’s time to put the concepts of true and false to work.

You can use an if statement to test an expression for truth and

execute some code based on it.

Here’s a simple form of the if statement:if (expression)

statement;

If expression is true, then statement is executed. Otherwise,

statement is skipped and the program branches to the

statement after the if suite.

;

Page 7: 6 < 7 || 7 >=8 11 > 5 - McLean County Unit District No. 5 · PDF file · 2016-03-13Set up p. 56 for Pre-Code and p. 57 for Cornell notes 57 3.2 The if Statement 56 Absolute Value

One-Way Selection (continued)

Page 8: 6 < 7 || 7 >=8 11 > 5 - McLean County Unit District No. 5 · PDF file · 2016-03-13Set up p. 56 for Pre-Code and p. 57 for Cornell notes 57 3.2 The if Statement 56 Absolute Value

Summary - do on p. 45Write some code that would

assign a grade of A if the score is

greater than or equal to 90.

HW - do on p. 42

#include <iostream>

using namespace std;

int main()

{

return 0;

}

do pre-code

on p. 56