Presentation is loading. Please wait.

Presentation is loading. Please wait.

LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.

Similar presentations


Presentation on theme: "LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel."— Presentation transcript:

1 LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel

2 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

3 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 Tr.Hadeel@hotmail.com

4 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 Tr.Hadeel@hotmail.com

5 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; Tr.Hadeel@hotmail.com

6 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 ”; Tr.Hadeel@hotmail.com 6

7 Question ? Which is the operator that provide the similar result of if..else double-selection statement? 7 Tr.Hadeel@hotmail.com

8 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; Tr.Hadeel@hotmail.com 8

9 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"; Tr.Hadeel@hotmail.com 9

10 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 Tr.Hadeel@hotmail.com

11 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 Tr.Hadeel@hotmail.com

12 Using bool Variables bool flag1 = true, flag2 = false; if ( flag1 ) … else … if ( flag1 || flag2 ) … … … else … … … 12 Tr.Hadeel@hotmail.com

13 Implicit Typecasting int x1 = -15, x2 = 0; if ( x1 ) … else … if ( x1 || x2 ) … … … else … … … 13 Tr.Hadeel@hotmail.com

14 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 !!! Tr.Hadeel@hotmail.com

15 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; } Tr.Hadeel@hotmail.com

16 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; } Tr.Hadeel@hotmail.com

17 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; } Tr.Hadeel@hotmail.com

18 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 Tr.Hadeel@hotmail.com

19 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 20 Tr.Hadeel@hotmail.com


Download ppt "LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel."

Similar presentations


Ads by Google