Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions."— Presentation transcript:

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

2 Chapter 4 slide 2 Topics 4.1 Relational Operators 4.2 The if Statement 4.3 The if/else Statement

3 Chapter 4 slide 3 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than or equal to <= Less than or equal to == Equal to != Not equal to

4 Chapter 4 slide 4 Relational Expressions Relational expressions are Boolean (i.e., evaluate to true or false) Examples: 12 > 5 is true 7 <= 5 is false if x is 10, then x == 10 is true, x != 8 is true, and x == 8 is false

5 Chapter 4 slide 5 Relational Expressions Can be assigned to a variable bool result = x <= y; Assigns 0 for false, 1 for true Do not confuse = and ==

6 Chapter 4 slide 6 4.2 The if Statement Allows statements to be conditionally executed or skipped over Models the way we mentally evaluate situations “If it is cold outside, wear a coat and wear a hat.”

7 Chapter 4 slide 7 Format of the if Statement if (expression) { statement1; statement2; … statementn; } The block inside the braces is called the body of the if statement. If there is only 1 statement in the body, the { } may be omitted. No ; goes here ; goes here

8 Chapter 4 slide 8 How the if Statement Works If (expression) is true, then the statement(s) in the body are executed. If (expression) is false, then the statement(s) are skipped.

9 Chapter 4 slide 9 if Statement Flow of Control expression 1 or more statements true false

10 Chapter 4 slide 10 Example if Statements if (score >= 60) cout << “You passed.\n”; if (score >= 90) { grade = 'A'; cout << “Wonderful job!\n"; }

11 Chapter 4 slide 11 Flags Variables that signal conditions Usually implemented as a bool The flag value can be both set and tested with if statements

12 Chapter 4 slide 12 Flag Example Example: bool validMonths = true; … if (months < 0) validMonths = false; … if (validMonths) moPayment = total / months;

13 Chapter 4 slide 13 4.3 The if/else Statement Allows a choice between statements depending on whether (expression) is true or false Format: if (expression) { statement set 1; } else { statement set 2; }

14 Chapter 4 slide 14 How the if/else Works If (expression) is true, statement set 1 is executed and statement set 2 is skipped. If (expression) is false, statement set 1 is skipped and statement set 2 is executed.

15 Chapter 4 slide 15 if/else Flow of Control expression statement set 1 true false statement set 2

16 Chapter 4 slide 16 Example if/else Statements if (score >= 60) cout << “You passed.\n”; else cout << “You did not pass.\n”; if (intRate > 0) { interest = loanAmt * intRate; cout << interest; } else cout << “You owe no interest.\n”;

17 Chapter 4 slide 17 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.”

18 Chapter 4 slide 18 if/else if Format if (expression) { statement set 1; } else if (expression) { statement set 2; } … else if (expression) { statement set n; }

19 Chapter 4 slide 19 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

20 Chapter 4 slide 20 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”;

21 Chapter 4 slide 21 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

22 Chapter 4 slide 22 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)

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


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

Similar presentations


Ads by Google