Download presentation
Presentation is loading. Please wait.
Published byAdam Wilkinson Modified over 8 years ago
1
Chapter 5 – Decision Making
2
Structured Programming Sequence Selection Repetition yesno yes no
3
Decision Statements (also known as Selection Statements) How to compare data values? Relational operators How to alter the sequence of program execution based on the result? if.. else statements How to deal with multiple choices? Switch statement
4
Example A teacher wants to assign letter grade based on percentage points. Percent range and the corresponding letter grade are as shown in the table below. Design a C++ solution (program) for this problem. Percent Range >=90>=80 < 90 >=70 <80>=60 <70<50 Letter Grade ABCDF
5
Relational Operators Operator Meaning < Less than ? > Greater than ? >= Greater than or equal to? <= Less than or equal to? == Equal to? != Not Equal to ? We will use relational operators to form relational expression of conditions.
6
Logical Operators !not &&and ||or These operators are used to combine more than one condition forming a complex condition.
7
Three Logical Operators Exclamation mark ! –NOT(negation) –unary Two ampersands && –AND(conjunction) –binary Two vertical pipes || –OR(inclusive disjunction) –binary Lesson 5.4
8
Logical NOT Operator Reverses result of relational expression Example: ! (x == y) Lesson 5.4 Evaluate relational expression, does 3 equal 7? 3 x 7 y ! ( false ) so negates to true 7 7 ! ( true ) so negates to false
9
Logical AND Operator Combines two relational expressions Combined expression true only if BOTH expressions are true Lesson 5.4 expression1 && expression2 true false false && true false && false true && true false true
10
Logical OR Operator Combines two relational expressions Combined expression false only if BOTH expressions are false Lesson 5.4 expression1 || expression2 true false true false || true false || false true || true true false true
11
Example: Assume: a = 4, b = -2, and c = 0 x = (a > b || b > c && a == b) x = ((a > b) || (b > c) && (a == b)) x = (TRUE || (FALSE && FALSE)) Group x = ((4 > -2) || (-2 > 0) && (4 == -2)) Group x = (TRUE || FALSE) x = (TRUE) x = 1 Lesson 5.5
12
if Statement An if statement allows a program to choose whether or not to execute a following statement. Syntax: (structure/format) if (condition) statement; Semantics: (meaning) condition is a Boolean expression: Something that evaluates to True or False. If condition is true then execute the statement.
13
The if statement : syntax if(expression) statement; //single statement executed //if expression is true if(expression) { //statements inside {} are //executed if expression is true statement1; statement2; … statement n; }
14
If -else Statement An if-else statement allows a program to do one thing if a condition is true and a different thing if the condition is false. Syntax: if ( condition ) statement1 else statement2 Statements to be executed for if and else can be a single statement or multiple statements enclosed in { }.
15
The if - else statement: syntax if(expression) statement; else statement; if(expression) { statement block } else { statement block }
16
Simple If/Else Statement Provides block of statements to be executed for either true OR false Braces optional if only one statement in block Lesson 5.2 if (expression) { statement1a; statement1b; … } else { statement1a; statement1b; … } if TRUEif FALSE
17
if Examples x = 3; if (x < 2) { cout << “x is smaller than 2”; } if (x < 12) cout << “smaller than twelve” else cout << “twelve or larger”; Simple if – statements only done on true, but when false, skips to end of if Full if – code for when true and also when false True False
18
if-else-if Form Lesson 5.6 if (relational_expression_1) { statement_block_1 } else if (relational_expression_2) { statement_block_2 }. else if (relational_expression_n-1) { statement_block_n-1 } else { statement_block_n } false else false else true This statement block would be executed!
19
The switch statement switch(expression) { case constant: statement(s); break; case constant: statement(s); break; /* default is optional*/ default: statement(s); }
20
The switch statement Expression must be of type integer or character The keyword case must be followed by a constant break statement is required unless you want all subsequent statements to be executed.
21
Practice! Convert these nested if/else statements to a switch statement : if (rank==1 || rank==2) cout << "Lower division \n"; else { if (rank==3 || rank==4) cout << "Upper division \n"; else { if (rank==5) cout << "Graduate student \n"; else cout << "Invalid rank \n"; }
22
Practice Solution! switch(rank) { case 1: case 2: cout << "Lower division \n"; break; case 3: case 4: cout << "Upper division \n"; break; case 5: cout << "Graduate student \n"; break; default: cout << "Invalid rank \n"; }//end switch
23
Summary In many applications, choices are to be made depending on some conditions related to the problem. Selection or decision structures are used to model such situations. C++ supports the implementation of “selection” through the “if” and “switch” statements. In this discussion we looked at various forms of selection statements.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.