Presentation is loading. Please wait.

Presentation is loading. Please wait.

STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.

Similar presentations


Presentation on theme: "STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else."— Presentation transcript:

1 STRUCTURED PROGRAMMING Selection Statements

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

3 Objectives 3  By the end you should:  Recognize the basic problem-solving techniques  Identify the problems that require the use of control structures  Differentiate between sequence, selection, and repetition structures  Differentiate between single, double, and multiple selection statements  Apply C++ syntax rules associated with each control structure  Develop programs that use different selection statements  Know how to use the if and if...else selection statements to choose among alternative actions.  Understand multiple selection using the switch selection statement  Know how to use the break control statement to exit from case label in switch statement  Recognize consequences of confusing the equality and assignment operators  Create, trace, and debug programs that use control structures

4 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 4

5 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 5

6 if single-selection Statement  Syntax  Single action  Multiple actions  If condition is true, action is performed  Otherwise, action is ignored 6 if (condition) { action1; action2;.. actionN; } if(grade >= 60) Cout << “Passed”; if(grade >= 60) Cout << “Passed”; if (condition) action;

7 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 7 if (condition) action1; else action2; if(grade >= 60) cout << “Passed”; else cout << “Passed”; if(grade >= 60) cout << “Passed”; else cout << “Passed”;

8 Question ? Which is the operator that provide the similar result of if..else double-selection statement? 8

9 Nested if.. else Statements  One inside another, test for multiple cases  Once condition met, other statements skipped 9 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;

10 Example 10 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";

11 Dangling- else Problem  Each else associated with immediately preceding if  There is exception when placing braces { } 11 x = 10; y = 2; if ( x > 5) if( y > 5) cout 5"<< endl; else cout<<"x is <=5"; x = 10; y = 2; if ( x > 5) if( y > 5) cout 5"<< endl; else cout<<"x is <=5"; Have logic error x = 10; y = 2; if ( x > 5) { if( y > 5) cout 5"<< endl; } else cout<<"x is <=5"; x = 10; y = 2; if ( x > 5) { if( y > 5) cout 5"<< endl; } else cout<<"x is <=5"; Correctness

12 Using bool Variables bool flag1,flag2; if ( flag1 ) … else … if ( flag1 || flag2 ) … … … else … … … 12

13 Implicit Typecasting int x1,x2; if ( x1 ) … else … if ( x1 || x2 ) … … … else … … … 13

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 !!!

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; }

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; }

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; }

18 Common Errors  Compilation errors  Placing semicolon (;) after if condition  Omitting spaces between case keyword and value  Specifying expression including variables (a + b) in case label of switch statement  Providing identical case labels  Logic errors  Placing semicolon (;) after else keyword  Forgetting a break statement when one is needed in a switch 18

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.

20 Included Sections 20 Chapter 4: from section 1 and 6 Chapter 5: section 6 and 9


Download ppt "STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else."

Similar presentations


Ads by Google