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 Tr.Hadeel@hotmail.com Control Structure (Logic Structure) 3 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 3. Decision structure Make choices by using relational or logical operators Case structure Enable to pick up one of a set of tasks Loop structure Enable to repeat tasks Tr.Hadeel@hotmail.com Selection Statements 4 Three types Single-selection statement Double-selection statement Select or ignore a single group of actions Select between two groups of actions Multiple-selection statement Select among many group of actions Tr.Hadeel@hotmail.com if single-selection Statement 5 Syntax Single action Multiple actions if (condition) action; if (condition) { action1; action2; .. actionN; } If condition is true, action is performed Otherwise, action is ignored Tr.Hadeel@hotmail.com if(grade >= 60) Cout << “Passed”; if..else double-selection Statement 6 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) action1; else action2; if(grade >= 60) cout << “Passed”; else cout << “failed ”; If condition is true, action that followed by if is performed Otherwise, action that followed by else is performed Tr.Hadeel@hotmail.com Question ? 7 Which is the operator that provide the similar result of if..else double-selection statement? Tr.Hadeel@hotmail.com Nested if..else Statements 8 One inside another, test for multiple cases Once condition met, other statements skipped 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; Tr.Hadeel@hotmail.com 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"; Tr.Hadeel@hotmail.com Dangling-else Problem 10 Each else associated with immediately preceding if There is exception when placing braces { } int x = 10, Have logic error y = 2; if ( x > 5) if( y > 5) cout << "x and y are > 5"<< endl; else cout<<"x is <=5“; Tr.Hadeel@hotmail.com int x = 10, y = 2; Correctness if ( x > 5) { if( y > 5) cout << "x and y are > 5"<< endl; } else cout<<"x is <=5"; Example:Determine the output for each of the following when x is 9 and y is 11 and when x is 11 and y 11 a) if ( x < 10 ) b) if ( x < 10 ) { if ( y > 10 ) if ( y > 10 ) cout << "*****" << endl; cout << "*****" << endl; else } cout << "#####" << endl; else { cout << "$$$$$" << endl; cout << "#####" << endl; ANS: cout << "$$$$$" << endl; } ANS: Tr.Hadeel@hotmail.com Using bool Variables 12 bool flag1 = true, flag2 = false; if ( flag1 ) … … else … … if ( flag1 || flag2 … … … else … … … ) Tr.Hadeel@hotmail.com Implicit Typecasting 13 int if x1 = -15, x2 = 0; ( x1 ) … … else … … if ( x1 || x2 ) … … … else … … … Tr.Hadeel@hotmail.com Confusing == 14 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”; This message will always be printed !!! if ( x=2 ) cout<<“x is equal to 2”; else cout<<“x is not equal to 2”; Tr.Hadeel@hotmail.com switch Multiple-selection 15 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 if (condition1) switch (expression) { case value1: action1; break; case value2: action2; break; ... break causes immediate exit from switch case valueN: actionN; break; default: statement action; } Tr.Hadeel@hotmail.com Example 16 switch (number) { case 0: cout << "Too small, sorry!"; break; case 5: cout << "Good job! " << endl; case 4: cout << "Nice Pick!" << endl; case 3: cout << "Excellent!" << endl; case 2: cout << "Masterful!" << endl; case 1: cout << "Incredible!" << endl; break; default:cout << "Too large!" << endl; break; } // // // // fall fall fall fall Tr.Hadeel@hotmail.com through through through through 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; } Tr.Hadeel@hotmail.com Common Compilation Errors 18 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 Tr.Hadeel@hotmail.com Exercise - 1 19 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. Tr.Hadeel@hotmail.com 20 End Tr.Hadeel@hotmail.com