Third Edition
Chapter 3
Making Decisions
Objectives
• Use the if and if-else statements
• Use nested if statements
• Avoid common pitfalls with if statements
• Use the switch statement
• Use the conditional operator
• Use the logical AND and the logical OR
• Make decisions with structure fields
Object-Oriented Programming Using C++, Third Edition 2
Using the if Statement
• The primary C++ selection structure statement used to perform a single-alternative selection is an if statement if ( Boolean expression ) action if true ;
• There is also a dual-alternative if if (Boolean expression) action if true; else action if false;
Object-Oriented Programming Using C++, Third Edition 3
Using the if Statement (continued)
Object-Oriented Programming Using C++, Third Edition 4
Using the if Statement (continued)
Object-Oriented Programming Using C++, Third Edition 5
Using the if Statement (continued)
Object-Oriented Programming Using C++, Third Edition 6
Using the if Statement (continued)
• Performing multiple tasks when a condition is met
Object-Oriented Programming Using C++, Third Edition 7
Using the if Statement (continued)
• Don’t forget the curly braces
Object-Oriented Programming Using C++, Third Edition 8
The Dual-Alternative if
• Also called the if-else structure
Object-Oriented Programming Using C++, Third Edition 9
Using a Nested if
• Nested if : if structure that rests entirely within another if structure —within either the if or the else clause
• Consider the code in the previous slide
– If the user enters ‘T’, it will display “Male”
– You can use a nested if to make the code more sophisticated
Object-Oriented Programming Using C++, Third Edition 10
Using a Nested if (continued)
Also called an if-else-if structure
Object-Oriented Programming Using C++, Third Edition 11
Avoiding Common Pitfalls with if
Statements
• Forgetting that C++ is case sensitive
• Assuming that indentation has a logical purpose
• Adding an unwanted semicolon
• Forgetting curly braces
• Using = instead of ==
See Figure 3-6
• Making unnecessary comparisons
• Creating unreachable code
Object-Oriented Programming Using C++, Third Edition 12
Pitfall: Forgetting that C++ is Case
Sensitive
• Code in Figure 3-10 displays “Invalid code” if a user enters ‘f’
Object-Oriented Programming Using C++, Third Edition 13
Pitfall: Adding an Unwanted Semicolon
Indentation has no logical purpose
Object-Oriented Programming Using C++, Third Edition 14
Pitfall: Using = instead of ==
Object-Oriented Programming Using C++, Third Edition 15
Pitfall: Making Unnecessary
Comparisons
Object-Oriented Programming Using C++, Third Edition 16
Pitfall: Making Unnecessary
Comparisons (continued)
Object-Oriented Programming Using C++, Third Edition 17
Pitfall: Creating Unreachable Code
Object-Oriented Programming Using C++, Third Edition 18
Pitfall: Creating Unreachable Code (continued)
Object-Oriented Programming Using C++, Third Edition 19
Using the switch Statement
• When you want to create different outcomes depending on specific values, you can use a series of if s
Object-Oriented Programming Using C++, Third Edition 20
Using the switch Statement
(continued)
• However, as an alternative to the long string of if s, you can use the switch statement
Removing break changes behavior of statement
Object-Oriented Programming Using C++, Third Edition 21
Using the Conditional Operator
• The conditional operator is represented by a question mark
• Provides a concise way to express two alternatives
– Alternative one: if(driverAge < 26) insurancePremium = 250; else insurancePremium = 185;
– Alternative two: driverAge < 26 ? insurancePremium = 250 : insurancePremium = 185;
• cout<<(a > b ? a : b)<<“ is greater”<<endl;
Object-Oriented Programming Using C++, Third Edition 22
Using the Logical AND
• Consider the following example
Object-Oriented Programming Using C++, Third Edition 23
Using the Logical AND (continued)
• You can use a single if statement that contains a logical AND , which you create by typing two ampersands (&&) between two Boolean expressions:
• Remember to include a complete Boolean expression on each side of the && if(payRate >= 6.00 && <= 12.00) // ERROR!
cout<<"Valid "<<endl;
Object-Oriented Programming Using C++, Third Edition 24
Using the Logical AND (continued)
Object-Oriented Programming Using C++, Third Edition 25
Using the Logical OR
• Consider the following example
Object-Oriented Programming Using C++, Third Edition 26
Using the Logical OR (continued)
• You can use a single if statement that contains a logical OR , which you create by typing two pipes (||) between two Boolean expressions
Object-Oriented Programming Using C++, Third Edition 27
Using the Logical OR (continued)
Object-Oriented Programming Using C++, Third Edition 28
Pitfall: Using OR when you mean AND
• Write a program given the following instructions:
“The user should enter 1 or 2. If the user doesn’t enter 1 or 2, issue an error message.”
• Incorrect: if(userResponse != 1 || userResponse != 2) cout<<"Incorrect entry"<<endl;
• Correct: if(userResponse != 1 && userResponse != 2) cout<<"Incorrect entry"<<endl;
Object-Oriented Programming Using C++, Third Edition 29
Combining AND and OR Selections
• AND takes precedence over OR
– AND is evaluated first
Object-Oriented Programming Using C++, Third Edition 30
Making Decisions with Structure Fields
Object-Oriented Programming Using C++, Third Edition 31
Object-Oriented Programming Using C++, Third Edition 32
You Do It: Using a Single-Alternative if cout<<"On average, how many hours are you gone from home each day? "; cin>>hoursGone; if(hoursGone > MANY_HOURS) cout<<"You should consider a cat"<<endl;
Object-Oriented Programming Using C++, Third Edition 33
if else cout<<"You can consider a dog"<<endl;
Object-Oriented Programming Using C++, Third Edition 34
Using a Compound Condition and
Nested if s
…
…
Object-Oriented Programming Using C++, Third Edition 35
Summary
• if statement is the primary selection structure statement to perform a single-alternative selection
• Dual-alternative if takes one action when its Boolean expression is evaluated as true, and uses an else clause to define the actions to take when the expression is evaluated as false
• A nested if rests entirely within another if structure
• Easy to make several types of mistakes
Object-Oriented Programming Using C++, Third Edition 36
Summary (continued)
• Use a series of if s or use the switch statement when you want to create different outcomes depending on specific values of a variable
• Conditional operator (?) provides a concise way to express two alternatives
• The logical AND is created by typing two ampersands (&&)
• Use a logical AND to create a compound Boolean expression in which two conditions must be true for the entire expression to evaluate as true
Object-Oriented Programming Using C++, Third Edition 37
Summary (continued)
• Logical OR is created by typing two pipes (||)
• Use a logical OR to create a compound Boolean expression in which at least one of two conditions must be true for the expression to evaluate as true
• When you combine an AND and an OR in the same expression, the AND takes precedence
• When you define a structure, and subsequently create objects that are instantiations of the structure, you use the individual structure’s fields the same way you use variables of the same type
Object-Oriented Programming Using C++, Third Edition 38