Presentation is loading. Please wait.

Presentation is loading. Please wait.

Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.

Similar presentations


Presentation on theme: "Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression."— Presentation transcript:

1 Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression can be type int or char, but not type double. First the expression is evaluated, then the list of case labels is searched until one matches the expression value. Statements following the matching case level are executed until a break statement is encountered. The break causes an exit from the switch statement. Execution continues with the statement that follows the closing brace of the switch statement body. If no case level matches the controlling expression value, the statement following the default label are executed. If there is no default label, the entire switch statement body is skipped.

2 Switch structure switch (grade) { case ‘A’ : cout << “Excellent” << endl; break; case ‘B’ : cout << “Good” << endl; break; case ‘C’ : cout << ”O.K.” << endl; break; case ‘D’ : cout << “Poor” << endl; break; case ‘F’ : cout << “Failed” << endl; break; default : cout << “Invalid letter grade” << endl; break; }

3 Case A Flowchart for switch structure Case B Case C Case D Case F Excellentbreak Good O.K. Poor Failed break invalid

4 Switch Structure How many lines of output will be produced by the following program fragment ? int x; x = 0; switch (x) { case 0 : cout << "got 0“ << endl; case 1 : cout << "got 1“ << endl; case 2 : cout << "got 2“ << endl; default : cout << "do not have 0, 1 or 2“ << endl; }

5 Switch Structure switch(grade) { case ‘A’ : case ‘B’ : cout << “Good Work”; break; case ‘C’ : cout << “Average Work”; break; case ‘D’ : case ‘F’ : cout <<“Poor Work”; break; default : cout << grade << “ is an invalid letter grade”; break; }

6 Switch and if /else if /else structure switch (grade) { case ‘A’ : case ‘B’ : cout << “Good Work”; break; case ‘C’ : cout << “Average Work”; break; case ‘D’ : case ‘F’ : cout <<“Poor Work”; break; default : cout << grade << “ is an invalid letter grade”; break; } if (grade == ‘A’ || grade == ‘B’) cout << “Good Work”; else if (grade == ‘C’) cout << “Average Work”; else if (grade == ‘D’ || grade == ‘F’) cout << “Poor Work”; else cout << grade << “ is an invalid letter grade”; switch if / else if /else

7 Switch and if /else if /else structure switch (a) { case 5 : c = c + b; case 2 : c = c + 2*b; break; case 3 : c = 7; break; case 6 : break; case 7 : c = c + 9; break; case 4 : case 1 : c *= c; break; default : c %= 2; break; } if (a == 5) { c = c + b; c = c + 2*b; } else if (a == 2) c = c +2*b; else if (a == 3) c = 7; else if (a == 6) ; else if (a == 7) c = c + 9; else if ((a == 4) || (a == 1) c * = c; else c % = 2; switch if / else if /else

8 if /else and switch structures if ((i > 0) && ( i < 5)) { if ( i > 2) { j = k + 3; ++k; } if ( i < 4) { j = k - 2; k += 3; } else j = k + 5; Switch (i) { case 1 : case 2 : j = k - 2; k += 3; break; case 3 : j = k + 3; ++k; j = k - 2; k += 3; break; case 4 : j = k + 3; ++k; break; default : j = k + 5; break; } if / else structureSwitch structure

9 Switch Structure What will be printed by the following code fragment if n is equals 4? switch (n) { case 1: cout << “One”; case 2: cout << “Two”; case 3: cout << “Three”; case 4: cout << “Four”; case 5: cout << “Five”; default: cout << “Out of the range”; } Output: FourFiveOut of the range

10 Do-While Statement Both the for statement and the while statement check a loop repetition condition before the first execution of the loop body. This pretest prevents the loop execution when there may be no data items to process or when the initial value of the loop control variable is outside the range. In most cases, this pretest is desirable. However, there are some situations (generally involving interactive input) when we know that a loop must execute at least once. The pseudocode for an input validation loop is as follows: 1. Get a data value 2. If data value in not in the range, go back to step 1.

11 Do-While Statement Syntax template for the do-while: do Statement while (Expression); Note: do-while ends with a semicolon.

12 Do-While Statement do { cout << “Enter a number from 1 through 5”; cin >> num; } while (num 5) Prompts the user to enter one of the numbers 1 through 5. After cin gets a number, the loop repetition condition checks whether num contains one of the numbers in the range. If so, the repetition condition will be false and the next statement after the loop will be executed. If num contains some other number, the condition will be true and the loop body will be repeated. Since we know the program user must enter at least one number, the do-while is an ideal statement to use to implement this loop.

13 The For Statement for (count = 1; count <= 10; count++) cout << count << endl; initialization Loop repetition condition Updating condition

14 Rewrite the code using while loop for (count = 1; count <= 10; count++) cout << count << endl; for loop count = 1; while(count <= 10) { cout << count << endl; count++; } While loop

15 The Break Statement #include int main() { int x; for (x = 1; x <= 10; x++) { if (x == 5) break; // break loop only if x == 5 cout << x << “ “; } cout << endl; cout << “Broke out loop at x == “ << x << endl; return 0; } 1 2 3 4 Broke out of loop at x == 5 Output

16 The Continue Statement #include int main() { int x; for (x = 1; x <= 10; x++) { if (x == 5) continue; // skip remaining code in loop // only if x == 5 cout << x << “ “; } cout << endl; cout << “Use continue to skip printing the value 5” << endl; return 0; } 1 2 3 4 6 7 8 9 10 Use continue to skip printing the value 5 Output


Download ppt "Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression."

Similar presentations


Ads by Google