LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel
Content 2 Control structures Types of selection statements if single-selection statement if..else double-selection statement Nested if..else statements Dangling else problem switch multiple-selection statement Common errors
Control Structure (Logic Structure) Used to design data flow in modules and program as a whole Basic structures 1. Sequential structure Processes one instruction after another 2. Selection structures Decision structure Make choices by using relational or logical operators Case structure Enable to pick up one of a set of tasks 3. Loop structure Enable to repeat tasks 3
Selection Statements Three types Single-selection statement Select or ignore a single group of actions Double-selection statement Select between two groups of actions Multiple-selection statement Select among many group of actions 4
if single-selection Statement Syntax Single action Multiple actions If condition is true, action is performed Otherwise, action is ignored 5 if (condition) { action1; action2;.. actionN; } if(grade >= 60) Cout << “Passed”; if(grade >= 60) Cout << “Passed”; if (condition) action;
if.. else double-selection Statement Begin with if followed by condition; then action or group of actions are listed End with else then action or group of actions are listed If condition is true, action that followed by if is performed Otherwise, action that followed by else is performed 6 if (condition) action1; else action2; if(grade >= 60) cout << “Passed”; else cout << “failed ”; if(grade >= 60) cout << “Passed”; else cout << “failed ”; 6
Question ? Which is the operator that provide the similar result of if..else double-selection statement? 7
Nested if.. else Statements One inside another, test for multiple cases Once condition met, other statements skipped 8 if (condition1) action1; else if (condition2) action2; else if (condition3) action3;... else actionN; if (condition1) { if (condition2) action1; else { if (condition3) action2; else action3; } else action4; 8
Example 9 if ( grade >= 90 ) cout << "A"; else if ( grade >= 80 ) cout << "B"; else if ( grade >= 70 ) cout << "C"; else if ( grade >= 60 ) cout << "D"; else cout << "F"; if ( grade >= 90 ) cout << "A"; else if ( grade >= 80 ) cout << "B"; else if ( grade >= 70 ) cout << "C"; else if ( grade >= 60 ) cout << "D"; else cout << "F"; 9
Dangling- else Problem Each else associated with immediately preceding if There is exception when placing braces { } 10 int x = 10, y = 2; if ( x > 5) if( y > 5) cout 5"<< endl; else cout<<"x is <=5“; int x = 10, y = 2; if ( x > 5) if( y > 5) cout 5"<< endl; else cout<<"x is <=5“; Have logic error int x = 10, y = 2; if ( x > 5) { if( y > 5) cout 5"<< endl; } else cout<<"x is <=5"; int x = 10, y = 2; if ( x > 5) { if( y > 5) cout 5"<< endl; } else cout<<"x is <=5"; Correctness
Example:Determine the output for each of the following when x is 9 and y is 11 and when x is 11 and y a) if ( x < 10 ) if ( y > 10 ) cout << "*****" << endl; else cout << "#####" << endl; cout << "$$$$$" << endl; ANS: b) if ( x < 10 ) { if ( y > 10 ) cout << "*****" << endl; } else { cout << "#####" << endl; cout << "$$$$$" << endl; } ANS: 11
Using bool Variables bool flag1 = true, flag2 = false; if ( flag1 ) … else … if ( flag1 || flag2 ) … … … else … … … 12
Implicit Typecasting int x1 = -15, x2 = 0; if ( x1 ) … else … if ( x1 || x2 ) … … … else … … … 13
Confusing == Confusing the equality operator == with the assignment operator = results in logic errors if ( x==2 ) cout<<“x is equal to 2”; else cout<<“x is not equal to 2”; if ( x=2 ) cout<<“x is equal to 2”; else cout<<“x is not equal to 2”; 14 This message will always be printed !!!
switch Multiple-selection Statement Perform actions based on possible values of variable or expression Begin with switch followed by controlling expression Value of expression compared to case labels then execute action for that case No matching, the execution go to the optional default statement break causes immediate exit from switch statement 15 if (condition1) switch (expression) { case value1: action1; break; case value2: action2; break;... case valueN: actionN; break; default: action; }
Example 16 switch (number) { case 0: cout << "Too small, sorry!"; break; case 5: cout << "Good job! " << endl; // fall through case 4: cout << "Nice Pick!" << endl; // fall through case 3: cout << "Excellent!" << endl; // fall through case 2: cout << "Masterful!" << endl; // fall through case 1: cout << "Incredible!" << endl; break; default:cout << "Too large!" << endl; break; } switch (number) { case 0: cout << "Too small, sorry!"; break; case 5: cout << "Good job! " << endl; // fall through case 4: cout << "Nice Pick!" << endl; // fall through case 3: cout << "Excellent!" << endl; // fall through case 2: cout << "Masterful!" << endl; // fall through case 1: cout << "Incredible!" << endl; break; default:cout << "Too large!" << endl; break; }
Printing Values of Enumerated Type 17 enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY }; Day today = FRIDAY ; switch (today) { case 0: cout << “MONDAY”; break; case 1: cout << “TUESDAY”; break; case 2: cout << “WEDNESDAY”; break; case 3: cout << “THURSDAY”; break; case 4: cout << “FRIDAY”; break; } enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY }; Day today = FRIDAY ; switch (today) { case 0: cout << “MONDAY”; break; case 1: cout << “TUESDAY”; break; case 2: cout << “WEDNESDAY”; break; case 3: cout << “THURSDAY”; break; case 4: cout << “FRIDAY”; break; }
Common Compilation Errors Placing semicolon (;) after if condition or else keyword Omitting spaces between case keyword and value Specifying expression including variables (a + b) in case label of switch statement Providing identical case labels Forgetting a break statement when one is needed in a switch 18
Exercise Write a program that asks for an integer and reports whether the number is odd or even. Use if.. else statement. Write another version of program using switch statement.
End 20