Professional Documents
Culture Documents
Tenth Edition
Chapter 4
Making Decisions
4-2
Topics 1 of 2
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-3
Topics 2 of 2
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-4
• A=B means the value of B (If B=5) is assigned to A, then after this the value of A is 5
as well. (Value of the right side is copied to the left side)
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-6
Relational Expressions 1 of 2
Relational Expressions 2 of 2
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-8
Operator Precedence
> >= < <= Highest
== != Lowest
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-9
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-10
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-12
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-13
Example if Statements
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-14
if Statement Notes
•(condition)must be in ( )
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-15
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-16
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-17
Flag
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-18
Flag Example
Example:
bool validMonths = true;
…
if (months < 0)
validMonths = false;
…
if (validMonths) //means if validMonths is true
// same as (validMonths==true)
monthlyPayment = total / months;
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-19
Integer Flags
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-22
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-23
if (intRate > 0)
{ interest = loanAmt * intRate;
cout << interest;
}
else
cout << "You owe no interest.\n";
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-24
if vs. if/else
If there are two conditions and both of them can be true or
both can be false, then use two if statements:
if (num > 0)
cout << num << " is positive\n";
if (num %2 == 0)
cout << num << " is even\n";
If the two conditions cannot both be true, then a single
if/else statement can work:
if (num %2 == 0)
cout << num << " Is even\n";
else
cout << num << " Is odd\n";
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-25
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-26
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-27
if/else if Format
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-28
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-29
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-30
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-31
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-32
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-33
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-34
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-35
Expression Value
(x > y) && (y > z) true or 1
(x > y) && (z > y) false or 0
(x <= z) || (y == z) false
(x <= z) || (y != z) true
!(x >= z) false
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-36
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-37
Short-Circuit Evaluation
•If an expression using the && operator is being
evaluated and the subexpression on the left side
is false, then there is no reason to evaluate the
subexpression on the right side. It is skipped.
•If an expression using the || operator is being
evaluated and the subexpression on the left side
is true, then there is no reason to evaluate the
right side. It is skipped.
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-38
Logical Precedence
Highest !
&&
Lowest ||
Example:
(2 < 3) || (5 > 6) && (7 > 8)
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-39
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-42
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-43
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
12-44
Testing Characters 1 of 2
FUNCTION MEANING
isalpha true if arg. is a letter, false otherwise
isalnum true if arg. is a letter or digit, false
otherwise
isdigit true if arg. is a digit 0-9, false otherwise
islower true if arg. is lowercase letter, false
otherwise
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
12-45
Testing Characters 2 of 2
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-47
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-48
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-49
switch (IntExpression)
{
case exp1: statement set 1;
case exp2: statement set 2;
...
case expn: statement set n;
default: statement set n+1;
}
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-50
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-51
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-53
switch (plusOrMinus)
{
case '+': cout << "plus";
break;
case '-': cout << "minus";
break;
default : cout << "invalid value";
}
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-54
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-55
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-57
Comparison of Enumerated
Data Type Values
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-58
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
4-59
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
3-60
Copyright
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved