(CH3) Chapter 3 PPT
(CH3) Chapter 3 PPT
false false
Boolean (radius >= 0)
Expression
true true
(a) (b)
Outer parentheses required Braces can be omitted if the block contains a single
statement
if ((i > 0) && (i < 10)) if ((i > 0) && (i < 10))
Equivalent
{ cout << "i is an " <<
cout << "i is an " << "integer between 0 and 10";
"integer between 0 and 10";
}
(a) (b)
SimpleIfDemo
true false
Boolean
Expression
Statement(s) for the true case Statement(s) for the false case
(a ) (b)
int i = 1; int i = 1;
int j = 2; int j = 2;
Equivalent
int k = 3; int k = 3;
if (i > j) if (i > j)
if (i > k) This is better if (i > k)
cout << "A"; with correct cout << "A";
else indentation else
cout << "B"; cout << "B";
(a) (b)
if (i > j)
{
if (i > k)
cout << "A";
}
else
cout << "B";
(a) (b)
(a) (b)
This is better
Equivalent if (even)
if (even == true)
cout <<"It is even."; cout << "It is even.";
(a) (b)
This is better
ComputeBMI
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
26
Example: Computing Taxes
The US federal personal income tax is calculated based on
the filing status and taxable income. There are four filing
statuses: single filers, married filing jointly, married filing
separately, and head of household. The tax rates for 2002
are shown in Table 3.6.
SubtractionQuiz
true false !(age > 18) is false, because (age > 18) is true.
false true !( weight == 150) is true, because (weight == 150) is false
false false false (age > 28) && (weight < 140) is false, because (age
false true false > 28) and (weight < 140) are both false.
false false false (age > 34) || (weight < 140) is false, because both (age
false true true > 34) and (weight < 140) are false.
true false true (age > 18) || (weight >= 150) is false, because (age >
true true true 18) is true.
TestBooleanOperators
LeapYear
status is 1
Compute tax for married file jointly break
status is 2
Compute tax for married file separatly break
status is 3
Compute tax for head of household break
default
Default actions
Next Statement
switch (day)
{
case 1: // Fall to through to the next case
case 2: // Fall to through to the next case
case 3: // Fall to through to the next case
case 4: // Fall to through to the next case
case 5: cout << "Weekday"; break;
case 0: // Fall to through to the next case
case 6: cout << "Weekend";
}
switch (day)
{
case 1: // Fall to through to the next case
case 2: // Fall to through to the next case
case 3: // Fall to through to the next case
case 4: // Fall to through to the next case
case 5: cout << "Weekday"; break;
case 0: // Fall to through to the next case
case 6: cout << "Weekend";
}
switch (day)
{
case 1: // Fall to through to the next case
case 2: // Fall to through to the next case
case 3: // Fall to through to the next case
case 4: // Fall to through to the next case
case 5: cout << "Weekday"; break;
case 0: // Fall to through to the next case
case 6: cout << "Weekend";
}
switch (day)
{
case 1: // Fall to through to the next case
case 2: // Fall to through to the next case
case 3: // Fall to through to the next case
case 4: // Fall to through to the next case
case 5: cout << "Weekday"; break;
case 0: // Fall to through to the next case
case 6: cout << "Weekend";
}
switch (day)
{
case 1: // Fall to through to the next case
case 2: // Fall to through to the next case
case 3: // Fall to through to the next case
case 4: // Fall to through to the next case
case 5: cout << "Weekday"; break;
case 0: // Fall to through to the next case
case 6: cout << "Weekend";
}
pig
0: monkey
rat
1: rooster
dog ox 2: dog
3: pig
rooster tiger 4: rat
year % 12 = 5: ox
monkey rabbit 6: tiger
7: rabbit
8: dragon
sheep dragon
9: s nake
horse snake 10: horse
11: sh eep
ChineseZodiac
is equivalent to
y = (x > 0) ? 1 : -1;
(booleanExpression) ? expression1 : expression2
Ternary operator
Binary operator
Unary operator
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
48
Conditional Operator
cout << ((num % 2 == 0) ? "num is even" :
"num is odd");
Day day;
The variable day can hold one of the values defined in the
enumerated type. For example, the following statement assigns
enumerated value MONDAY to variable day:
day = MONDAY;
Enumerated Types
As with any other type, you can declare and initialize a variable in
one statement:
TestEnumeratedType