+ All Categories
Home > Documents > Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Date post: 13-Jan-2016
Category:
Upload: esmond-brooks
View: 219 times
Download: 0 times
Share this document with a friend
38
Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions
Transcript
Page 1: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++Third Edition

Chapter 3Making Decisions

Page 2: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 2

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

Page 3: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 3

Using the if Statement

• The primary C++ selection structure statement used to perform a single-alternative selection is an if statementif (Boolean expression)

action if true;

• There is also a dual-alternative ifif (Boolean expression)

action if true;

else

action if false;

Page 4: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 4

Using the if Statement (continued)

Page 5: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 5

Using the if Statement (continued)

Page 6: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 6

Using the if Statement (continued)

Page 7: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 7

Using the if Statement (continued)• Performing multiple tasks when a condition is

met

Page 8: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 8

Using the if Statement (continued)• Don’t forget the curly braces

Page 9: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 9

The Dual-Alternative if

• Also called the if-else structure

Page 10: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 10

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

Page 11: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 11

Also called an if-else-if structure

Using a Nested if (continued)

Page 12: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 12

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

Page 13: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 13

Pitfall: Forgetting that C++ is Case Sensitive

• Code in Figure 3-10 displays “Invalid code” if a user enters ‘f’

Page 14: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 14

Pitfall: Adding an Unwanted Semicolon

Indentation has no logical purpose

Page 15: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 15

Pitfall: Using = instead of ==

Page 16: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 16

Pitfall: Making Unnecessary Comparisons

Page 17: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 17

Pitfall: Making Unnecessary Comparisons (continued)

Page 18: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 18

Pitfall: Creating Unreachable Code

Page 19: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 19

Pitfall: Creating Unreachable Code (continued)

Page 20: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 20

Using the switch Statement

• When you want to create different outcomes depending on specific values, you can use a series of ifs

Page 21: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 21

Using the switch Statement (continued)

• However, as an alternative to the long string of ifs, you can use the switch statement

Removing break changes behavior of statement

Page 22: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 22

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;

Page 23: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 23

Using the Logical AND

• Consider the following example

Page 24: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 24

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;

Page 25: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 25

Using the Logical AND (continued)

Page 26: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 26

Using the Logical OR

• Consider the following example

Page 27: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 27

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

Page 28: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 28

Using the Logical OR (continued)

Page 29: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 29

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;

Page 30: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 30

Combining AND and OR Selections

• AND takes precedence over OR– AND is evaluated first

Page 31: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 31

Making Decisions with Structure Fields

Page 32: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 32

Page 33: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 33

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;

Page 34: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 34

Using a Dual-Alternative if

else

cout<<"You can consider a dog"<<endl;

Page 35: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 35

Using a Compound Condition and Nested ifs

Page 36: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 36

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

Page 37: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 37

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

Page 38: Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Object-Oriented Programming Using C++, Third Edition 38

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


Recommended