380 likes | 522 Views
Object-Oriented Programming Using C++ 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
E N D
Object-Oriented Programming Using C++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
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
Using the if Statement (continued) Object-Oriented Programming Using C++, Third Edition
Using the if Statement (continued) Object-Oriented Programming Using C++, Third Edition
Using the if Statement (continued) Object-Oriented Programming Using C++, Third Edition
Using the if Statement (continued) • Performing multiple tasks when a condition is met Object-Oriented Programming Using C++, Third Edition
Using the if Statement (continued) • Don’t forget the curly braces Object-Oriented Programming Using C++, Third Edition
The Dual-Alternative if • Also called the if-else structure Object-Oriented Programming Using C++, Third Edition
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
Using a Nested if (continued) Also called an if-else-if structure Object-Oriented Programming Using C++, Third Edition
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 Object-Oriented Programming Using C++, Third Edition
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
Indentation has no logical purpose Pitfall: Adding an Unwanted Semicolon Object-Oriented Programming Using C++, Third Edition
Pitfall: Using = instead of == Object-Oriented Programming Using C++, Third Edition
Pitfall: Making Unnecessary Comparisons Object-Oriented Programming Using C++, Third Edition
Pitfall: Making Unnecessary Comparisons (continued) Object-Oriented Programming Using C++, Third Edition
Pitfall: Creating Unreachable Code Object-Oriented Programming Using C++, Third Edition
Pitfall: Creating Unreachable Code (continued) Object-Oriented Programming Using C++, Third Edition
Using the switch Statement • When you want to create different outcomes depending on specific values, you can use a series of ifs Object-Oriented Programming Using C++, Third Edition
Removing break changes behavior of statement Using the switch Statement (continued) • However, as an alternative to the long string of ifs, you can use the switch statement Object-Oriented Programming Using C++, Third Edition
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
Using the Logical AND • Consider the following example Object-Oriented Programming Using C++, Third Edition
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
Using the Logical AND (continued) Object-Oriented Programming Using C++, Third Edition
Using the Logical OR • Consider the following example Object-Oriented Programming Using C++, Third Edition
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
Using the Logical OR (continued) Object-Oriented Programming Using C++, Third Edition
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
Combining AND and OR Selections • AND takes precedence over OR • AND is evaluated first Object-Oriented Programming Using C++, Third Edition
Making Decisions with Structure Fields Object-Oriented Programming Using C++, Third Edition
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
Using a Dual-Alternative if else cout<<"You can consider a dog"<<endl; Object-Oriented Programming Using C++, Third Edition
Using a Compound Condition and Nested ifs … … Object-Oriented Programming Using C++, Third Edition
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
Summary (continued) • Use a series of ifs 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
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