Presentation is loading. Please wait.

Presentation is loading. Please wait.

Alternate Version of STARTING OUT WITH C++ 4th Edition

Similar presentations


Presentation on theme: "Alternate Version of STARTING OUT WITH C++ 4th Edition"— Presentation transcript:

1 Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 4 Making Decisions Copyright 2004 Scott/Jones Publishing

2 Topics 4.4 The if/else if Statement 4.5 Menus 4.6 Nested if Statements
4.7 Logical Operators 4.8 Checking Numeric Ranges with Logical Operators Chapter 4 slide 2

3 4.4 The if/else if Statement
Chain of if statements that test in order until one is found to be true Also models thought processes “If it is raining, take an umbrella, else, if it is windy, take a hat, else, if it is sunny, take sunglasses.” Chapter 4 slide 3

4 if/else if Format if (expression) { statement set 1; }
else if (expression) { statement set 2; { statement set n; See pr4-07.cpp Chapter 4 slide 4

5 Using a Trailing else Used with if/else if statement when none of (expression) is true Provides a default statement/action Can be used to catch invalid values or handle other exceptional situations Chapter 4 slide 5

6 Example if/else if with Trailing else
if (age >= 21) cout << “Adult”; else if (age >= 13) cout << “Teen”; else if (age >= 2) cout << “Child”; else cout << “Baby”; See pr4-09.cpp Chapter 4 slide 6

7 4.5 Menus Menu-driven program: program execution controlled by user selecting from a list of actions Menu: list of choices on the screen Can be implemented using if/else if statements Chapter 4 slide 7

8 Menu-driven program organization
Display list of numbered or lettered choices for actions. Input user’s selection Test user selection in (expression) if a match, then execute code to carry out desired action if not, then test with next (expression) See pr4-10.cpp Chapter 4 slide 8

9 4.6 Nested if Statements An if statement that is part of the if or else part of another if statement Can be used to evaluate > 1 data item or condition if (score < 100) { if (score > 90) grade = 'A'; } Chapter 4 slide 9

10 Notes on Coding Nested ifs
An else matches the nearest if that does not have an else if (score < 100) if (score > 90) grade = 'A'; else ... // goes with second if, // not first one Proper indentation helps greatly See pr4-11.cpp and pr4-12.cpp Chapter 4 slide 10

11 Operators, Meaning, and Explanation
4.7 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 Chapter 4 slide 11

12 Logical Operator Examples
int x = 12, y = 5, z = -4; (x > y) && (y > z) true (x > y) && (z > y) false (x <= z) || (y == z) (x <= z) || (y != z) !(x >= z) See pr4-13.cpp, pr4-14.cpp, and pr4-15.cpp Chapter 4 slide 12

13 Logical Precedence Example: (2 < 3) || (5 > 6) && (7 > 8)
Highest ! && Lowest || Example: (2 < 3) || (5 > 6) && (7 > 8) is true because AND is done before OR Chapter 4 slide 13

14 More on Precedence Highest arithmetic operators relational operators
Lowest logical operators Example: 8 < || 5 == is true Chapter 4 slide 14

15 Logical Operator Notes
Short circuit evaluation If the value of an expression can be determined by evaluating just the sub-expression on left side of a logical operator, the sub-expression on the right side is not evaluated True OR anything is true False AND anything is false Chapter 4 slide 15

16 4.8 Checking Numeric Ranges with Logical Operators
Used to test if a value is within a range if (grade >= 0 && grade <= 100) cout << "Valid grade"; Can also test if a value lies outside a range if (grade <= 0 || grade >= 100) cout << "Invalid grade"; Cannot use mathematical notation if (0 <= grade <= 100) //Doesn’t //work! Chapter 4 slide 16

17 Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 4 Making Decisions Copyright 2004 Scott/Jones Publishing


Download ppt "Alternate Version of STARTING OUT WITH C++ 4th Edition"

Similar presentations


Ads by Google