6 < 7 || 7 >=8 11 > 5 - McLean County Unit District No. 5 · PDF file ·...
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](https://reader034.fdocuments.us/reader034/viewer/2022051722/5aaa0b7e7f8b9a6c188db5a2/html5/thumbnails/1.jpg)
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 PreCode
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](https://reader034.fdocuments.us/reader034/viewer/2022051722/5aaa0b7e7f8b9a6c188db5a2/html5/thumbnails/2.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022051722/5aaa0b7e7f8b9a6c188db5a2/html5/thumbnails/3.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022051722/5aaa0b7e7f8b9a6c188db5a2/html5/thumbnails/4.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022051722/5aaa0b7e7f8b9a6c188db5a2/html5/thumbnails/5.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022051722/5aaa0b7e7f8b9a6c188db5a2/html5/thumbnails/6.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022051722/5aaa0b7e7f8b9a6c188db5a2/html5/thumbnails/7.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022051722/5aaa0b7e7f8b9a6c188db5a2/html5/thumbnails/8.jpg)
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