Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2006 Pearson Education, Inc. All rights reserved. 1 4.6 if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.

Similar presentations


Presentation on theme: " 2006 Pearson Education, Inc. All rights reserved. 1 4.6 if…else Double-Selection Statement if – Performs action if condition true if…else – Performs."— Presentation transcript:

1  2006 Pearson Education, Inc. All rights reserved. 1 4.6 if…else Double-Selection Statement if – Performs action if condition true if…else – Performs one action if condition is true, a different action if it is false Pseudocode – If student’s grade is greater than or equal to 60 print “Passed” Else print “Failed” C++ code – if ( grade >= 60 ) cout << "Passed"; else cout << "Failed";

2  2006 Pearson Education, Inc. All rights reserved. 2 4.6 if…else Double-Selection Statement (Cont.) Ternary conditional operator ( ?: ) – Three arguments (condition, value if true, value if false ) Code could be written: – cout = 60 ? “Passed” : “Failed” ); ConditionValue if trueValue if false

3  2006 Pearson Education, Inc. All rights reserved. 3 4.6 if…else Double-Selection Statement (Cont.) Nested if…else statements – One inside another, test for multiple cases – Once a condition met, other statements are skipped – Example If student’s grade is greater than or equal to 90 Print “A” Else If student’s grade is greater than or equal to 80 Print “B” Else If student’s grade is greater than or equal to 70 Print “C” Else If student’s grade is greater than or equal to 60 Print “D” Else Print “F”

4  2006 Pearson Education, Inc. All rights reserved. 4 4.6 if…else Double-Selection Statement (Cont.) Nested if…else statements (Cont.) – Written In C++ if ( studentGrade >= 90 ) cout = 80 ) cout = 70 ) cout = 60 ) cout << "D"; else cout << "F";

5  2006 Pearson Education, Inc. All rights reserved. 5 4.6 if…else Double-Selection Statement (Cont.) Nested if…else statements (Cont.) – Written In C++ (indented differently) if ( studentGrade >= 90 ) cout = 80 ) cout = 70 ) cout = 60 ) cout << "D"; else cout << "F";

6  2006 Pearson Education, Inc. All rights reserved. 6 4.6 if…else Double-Selection Statement (Cont.) Dangling- else problem – Compiler associates else with the immediately preceding if – Example if ( x > 5 ) if ( y > 5 ) cout 5"; else cout << "x is <= 5"; – Compiler interprets as if ( x > 5 ) if ( y > 5 ) cout 5"; else cout << "x is <= 5";

7  2006 Pearson Education, Inc. All rights reserved. 7 4.6 if…else Double-Selection Statement (Cont.) Dangling- else problem (Cont.) – Rewrite with braces ( {} ) if ( x > 5 ) { if ( y > 5 ) cout 5"; } else cout << "x is <= 5"; – Braces indicate that the second if statement is in the body of the first and the else is associated with the first if statement

8  2006 Pearson Education, Inc. All rights reserved. 8 4.6 if…else Double-Selection Statement (Cont.) Compound statement – Also called a block Set of statements within a pair of braces Used to include multiple statements in an if body – Example if ( studentGrade >= 60 ) cout << "Passed.\n"; else { cout << "Failed.\n"; cout << "You must take this course again.\n"; } – Without braces, cout << "You must take this course again.\n"; always executes

9  2006 Pearson Education, Inc. All rights reserved. 9 Outline fig04_21.cpp (1 of 1) Postincrementing the c variablePreincrementing the c variable

10  2006 Pearson Education, Inc. All rights reserved. 10 4.12 Increment and Decrement Operators (Cont.) If c = 5, then – cout << ++c; c is changed to 6 Then prints out 6 – cout << c++; Prints out 5 ( cout is executed before the increment) c then becomes 6

11  2006 Pearson Education, Inc. All rights reserved. 11 4.12 Increment and Decrement Operators (Cont.) When variable is not in an expression – Preincrementing and postincrementing have same effect Example – ++c; cout << c; and c++; cout << c; are the same

12  2006 Pearson Education, Inc. All rights reserved. 12 5.8 Logical Operators (Cont.) Logical Negation ( ! ) Operator – Unary operator – Returns true when its operand is false, and vice versa – Example if ( !( grade == sentinelValue ) ) cout << "The next grade is " << grade << endl; is equivalent to: if ( grade != sentinelValue ) cout << "The next grade is " << grade << endl; Stream manipulator boolalpha – Display bool expressions in words, “true” or “false”

13  2006 Pearson Education, Inc. All rights reserved. 13 5.9 Confusing Equality ( == ) and Assignment ( = ) Operators (Cont.) Example if ( payCode == 4 ) cout << "You get a bonus!" << endl; – If paycode is 4, bonus is given If == was replaced with = if ( payCode = 4 ) cout << "You get a bonus!" << endl; – paycode is set to 4 (no matter what it was before) – Condition is true (since 4 is non-zero) Bonus given in every case

14  2006 Pearson Education, Inc. All rights reserved. 14 Conditional Operator if (a>=b) max=a; else max=b; Can be rewritten as: max= (a>=b) ? a : b;

15  2006 Pearson Education, Inc. All rights reserved. 15 Switch Structure When the value of the switch expression matches a case value, all statements execute until a break is encountered, and the program skips all case labels in between.

16  2006 Pearson Education, Inc. All rights reserved. 16 Switch Structure – Example 1 switch (num) { case 0: case 1: cout << “Hello “; case 2: cout << “there.”; case 3: cout << “I am “; case 4: cout << “Mickey.”<< endl; break; case 5: cout << “How “; case 6: case 7: case 8: cout << “are you?” << endl; break; case 9: break; case 10: cout << “Have a nice day.”<<endl; default: cout << “Wrong number.” << endl; }

17  2006 Pearson Education, Inc. All rights reserved. 17 Switch Structure – Example 2 switch (age >=18) { case 1: // case true: cout << “Age more than 18” << endl; break; case 0: // case false: cout << “Age less than 18” << endl; break; }


Download ppt " 2006 Pearson Education, Inc. All rights reserved. 1 4.6 if…else Double-Selection Statement if – Performs action if condition true if…else – Performs."

Similar presentations


Ads by Google