Download presentation
Presentation is loading. Please wait.
Published byEsmond Brooks Modified over 9 years ago
1
Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions
2
Object-Oriented Programming Using C++, Third Edition2 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
3
Object-Oriented Programming Using C++, Third Edition3 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;
4
Object-Oriented Programming Using C++, Third Edition4 Using the if Statement (continued)
5
Object-Oriented Programming Using C++, Third Edition5 Using the if Statement (continued)
6
Object-Oriented Programming Using C++, Third Edition6 Using the if Statement (continued)
7
Object-Oriented Programming Using C++, Third Edition7 Using the if Statement (continued) Performing multiple tasks when a condition is met
8
Object-Oriented Programming Using C++, Third Edition8 Using the if Statement (continued) Don’t forget the curly braces
9
Object-Oriented Programming Using C++, Third Edition9 The Dual-Alternative if Also called the if-else structure
10
Object-Oriented Programming Using C++, Third Edition10 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
11
Object-Oriented Programming Using C++, Third Edition11 Also called an if-else-if structure Using a Nested if (continued)
12
Object-Oriented Programming Using C++, Third Edition12 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 == Making unnecessary comparisons Creating unreachable code See Figure 3-6
13
Object-Oriented Programming Using C++, Third Edition13 Pitfall: Forgetting that C++ is Case Sensitive Code in Figure 3-10 displays “Invalid code” if a user enters ‘f’
14
Object-Oriented Programming Using C++, Third Edition14 Pitfall: Adding an Unwanted Semicolon Indentation has no logical purpose
15
Object-Oriented Programming Using C++, Third Edition15 Pitfall: Using = instead of ==
16
Object-Oriented Programming Using C++, Third Edition16 Pitfall: Making Unnecessary Comparisons
17
Object-Oriented Programming Using C++, Third Edition17 Pitfall: Making Unnecessary Comparisons (continued)
18
Object-Oriented Programming Using C++, Third Edition18 Pitfall: Creating Unreachable Code
19
Object-Oriented Programming Using C++, Third Edition19 Pitfall: Creating Unreachable Code (continued)
20
Object-Oriented Programming Using C++, Third Edition20 Using the switch Statement When you want to create different outcomes depending on specific values, you can use a series of if s
21
Object-Oriented Programming Using C++, Third Edition21 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
22
Object-Oriented Programming Using C++, Third Edition22 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 b ? a : b)<<“ is greater”<<endl;
23
Object-Oriented Programming Using C++, Third Edition23 Using the Logical AND Consider the following example
24
Object-Oriented Programming Using C++, Third Edition24 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;
25
Object-Oriented Programming Using C++, Third Edition25 Using the Logical AND (continued)
26
Object-Oriented Programming Using C++, Third Edition26 Using the Logical OR Consider the following example
27
Object-Oriented Programming Using C++, Third Edition27 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
28
Object-Oriented Programming Using C++, Third Edition28 Using the Logical OR (continued)
29
Object-Oriented Programming Using C++, Third Edition29 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;
30
Object-Oriented Programming Using C++, Third Edition30 Combining AND and OR Selections AND takes precedence over OR –AND is evaluated first
31
Object-Oriented Programming Using C++, Third Edition31 Making Decisions with Structure Fields
32
Object-Oriented Programming Using C++, Third Edition32
33
Object-Oriented Programming Using C++, Third Edition33 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;
34
Object-Oriented Programming Using C++, Third Edition34 Using a Dual-Alternative if else cout<<"You can consider a dog"<<endl;
35
Object-Oriented Programming Using C++, Third Edition35 Using a Compound Condition and Nested if s … …
36
Object-Oriented Programming Using C++, Third Edition36 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
37
Object-Oriented Programming Using C++, Third Edition37 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
38
Object-Oriented Programming Using C++, Third Edition38 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.