Switch structure Switch structure selects one from several

  • Slides: 16
Download presentation
Switch structure • Switch structure selects one from several alternatives depending on the value

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.

Switch structure switch (grade) { case ‘A’ : cout << “Excellent” << endl; break;

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

Flowchart for switch structure Case A Excellent break Case B Good break Case C

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

Switch Structure How many lines of output will be produced by the following program

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

Switch Structure switch(grade) { case ‘A’ : case ‘B’ : cout << “Good Work”;

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

Switch and if /else structure switch (grade) { case ‘A’ : case ‘B’ :

Switch and 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 / else if /else 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 and if /else structure switch (a) { case 5 : c = c

Switch and 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 / else if /else 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;

if /else and switch structures if / else structure if ((i > 0) &&

if /else and switch structures if / else structure 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 structure 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; }

Switch Structure What will be printed by the following code fragment if n is

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: Four. Five. Out of the range

Do-While Statement • Both the for statement and the while statement check a loop

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.

Do-While Statement Syntax template for the do-while: do Statement while (Expression); Note: do-while ends

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

Do-While Statement do { cout << “Enter a number from 1 through 5”; cin

Do-While Statement do { cout << “Enter a number from 1 through 5”; cin >> num; } while (num < 1 || 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.

The For Statement Loop repetition condition for (count = 1; count <= 10; count++)

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

Rewrite the code using while loop for (count = 1; count <= 10; count++)

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

The Break Statement 1. #include <iostream> Output 2. int main() 1234 3. { Broke

The Break Statement 1. #include <iostream> Output 2. int main() 1234 3. { Broke out of loop at x == 5 4. int x; 5. for (x = 1; x <= 10; x++) 6. { 7. if (x == 5) 8. break; // break loop only if x == 5 9. cout << x << “ “; 10. } 11. 12. cout << endl; 13. cout << “Broke out loop at x == “ << x << endl; 14. return 0; 15. }

The Continue Statement #include <iostream> Output int main() 1 2 3 4 6 7

The Continue Statement #include <iostream> Output int main() 1 2 3 4 6 7 8 9 10 { Use continue to skip printing the value 5 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; }