Chapter 4 Decision Making
Chapter 4 Decision Making
1
Chapter 4:
Making
Decisions
2
4.1
Relational Operators
3
Relational Operators
• Used to compare numbers to determine
relative order
• Operators:
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
== Equal to
!= Not equal to
4
Relational Expressions
• Boolean expressions – true or false
• Examples:
12 > 5 is true
7 <= 5 is false
if x is 10, then
x == 10 is true,
x != 8 is true, and
x == 8 is false
5
Relational Expressions
• Can be assigned to a variable:
result = x <= y;
• Assigns 0 for false, 1 for true
• Do not confuse = and ==
6
4.2
The if Statement
7
The if Statement
• Allows statements to be conditionally
executed or skipped over
• Models the way we mentally evaluate
situations:
– "If it is raining, take an umbrella."
– "If it is cold outside, wear a coat."
8
Flowchart for Evaluating a Decision
9
The if Statement
• General Format:
if (expression)
statement;
10
The if Statement-What Happens
To evaluate:
if (expression)
statement;
• If the expression is true, then
statement is executed.
• If the expression is false, then
statement is skipped.
11
if Statement in Program 4-2
Continued…
12
if Statement in Program 4-2
13
Flowchart for Program 4-2 Lines 21
and 22
14
if Statement Notes
• Do not place ; after (expression)
• Place statement; on a separate line
after (expression), indented:
if (score > 90)
grade = 'A';
• Be careful testing floats and doubles
for equality
• 0 is false; any other value is true
15
4.3
Expanding the if Statement
16
Expanding the if Statement
• To execute more than one statement as part of
an if statement, enclose them in { }:
if (score > 90)
{
grade = 'A';
cout << "Good Job!\n";
}
• { } creates a block of code
17
4.4
The if/else Statement
18
The if/else statement
• Provides two possible paths of execution
• Performs one statement or block if the
expression is true, otherwise performs
another statement or block.
• General Format:
if (expression)
statement1; // or block
else
statement2; // or block
19
if/else-What Happens
To evaluate:
if (expression)
statement1;
else
statement2;
20
if/else-What Happens
21
The if/else statement and
Modulus Operator in Program 4-8
22
Flowchart for Program 4-8 Lines 14
through 18
23
Testing the Divisor in Program 4-9
Continued…
24
Testing the Divisor in Program 4-9
25
The if/else if/else statement
• General Format:
if (expression)
statement1; // or block1
else if (expression)
statement2; // or block2
else
statement3; // or block2
26
The if/else if/else statement
27
The if/else if/else statement
28
4.5
Nested if Statements
29
Nested if Statements
• An if statement that is nested inside
another if statement
• Nested if statements can be used to test
more than one condition
30
Flowchart for a Nested if
Statement
31
Nested if Statements
32
Nested if Statements
33
Use Proper Indentation!
34
4.6
The if/else if Statement
35
The if/else if Statement
• Tests a series of conditions until one is found to
be true
• Often simpler than using nested if/else
statements
• Can be used to model thought processes such
as:
36
if/else if Format
if (expression)
statement1; // or block
else if (expression)
statement2; // or block
.
. // other else ifs
.
else if (expression)
statementn; // or block
37
The if/else if Statement
38
Using a Trailing else to Catch
Errors
• The trailing else clause is optional, but it
is best used to catch errors.
This trailing
else
catches
invalid test
scores
39
4.7
Flags
40
Flags
• Variable that signals a condition
• Usually implemented as a bool variable
• Can also be an integer
– The value 0 is considered false
– Any nonzero value is considered true
• As with other variables in functions, must
be assigned an initial value before it is
used
41
• #include <iostream>
Flags •
•
using namespace std;
int main() {
• int number;
• cout << "Enter a number: ";
• cin >> number;
• // Initialize flag variable
• bool is_positive = false;
• // Check if the number is positive
• if (number > 0) {
• is_positive = true; // Set the flag to true if the number is positive
• }
• // Check the flag and print result
• if (is_positive) {
• cout << "The number is positive." << endl;
• } else {
• cout << "The number is not positive." << endl;
• }
• return 0;
• }
42
4.8
Logical Operators
43
Logical Operators
• Used to create relational expressions from
other relational expressions
• Operators, meaning, and explanation:
&& AND New relational expression is true if both
expressions are true
|| OR New relational expression is true if either
expression is true
! NOT Reverses the value of an expression – true
expression becomes false, and false becomes
true
44
Logical Operators-Examples
int x = 12, y = 5, z = -4;
(x > y) && (y > z) true
(x <= z) || (y == z) false
(x <= z) || (y != z) true
45
The logical && operator
46
The logical || Operator
47
The logical! Operator
48
Checking Numeric Ranges with
Logical Operators
• Used to test to see if a value falls inside a range:
if (grade >= 0 && grade <= 100)
cout << "Valid grade";
• Can also test to see if value falls outside of range:
if (grade <= 0 || grade >= 100)
cout << "Invalid grade";
• Cannot use mathematical notation:
if (0 <= grade <= 100) //doesn’t work!
49
4.9
The Conditional Operator
50
The Conditional Operator
• Can use to create short if/else
statements
• Format: expr ? expr : expr;
51
The Conditional Operator
• The value of a conditional expression is
– The value of the second expression if the first
expression is true
– The value of the third expression if the first
expression is false
• Parentheses () may be needed in an
expression due to precedence of
conditional operator
52
The Conditional Operator
53
4.10
The switch Statement
54
The switch Statement
• Used to select among statements from
several alternatives
• In some cases, can be used instead of
if/else if statements
55
switch Statement Format
switch (expression) //integer
{
case exp1: statement1;
case exp2: statement2;
...
case expn: statementn;
default: statementn+1;
}
56
switch Statement Format
57
The switch Statement
58
switch Statement Requirements
1) expression must be an integer variable
or an expression that evaluates to an
integer value
2) exp1 through expn must be constant
integer expressions or literals, and must
be unique in the switch statement
3) default is optional but recommended
59
switch Statement-How it Works
1) expression is evaluated
2) The value of expression is compared
against exp1 through expn.
3) If expression matches value expi, the
program branches to the statement
following expi and continues to the end
of the switch
4) If no matching value is found, the
program branches to the statement after
default:
60
break Statement
• Used to exit a switch statement
• If it is left out, the program "falls through"
the remaining statements in the switch
statement
61
break Statement – Not Used
62
break Statement - Used
63
break and default statements
Continued…
64
break and default statements in
Program 4-25
65
Using switch in Menu Systems
• switch statement is a natural choice for
menu-driven program:
– display the menu
– then, get the user's menu selection
– use user input as expression in switch
statement
– use menu choices as expr in case
statements
66
JAZAK ALLAH!
Any Question?