1 / 12

Mastering Control Structures in Programming

Explore switch statement for multiway branching and efficient coding. Learn about do-while loops, for loops, and break/continue statements.

petersonr
Download Presentation

Mastering Control Structures in Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 9 More Control Structures

  2. Switch Statement • A multiway branching control structure • An alternative to nested if statements • Uses a switch expression and cases to determine which branch to take • Switch Expression: • The expression whose value determines which switch label is selected. It cannot be a floating-point or a string.

  3. Example switch ( letter ) { case ‘X’: Statement1; break; case ‘L’: case ‘M’: Statement2; break; case ‘S’: Statement3; break; default: Statement4; } Statement5;

  4. Details • The switch statement must be of constant type, i.e., char, short, int, long, bool, or enum. • What comes after the case label must match in type either directly or through coercion to the type of the switch expression. • The default label is optional.

  5. switch vs if switch ( grade ) { case ‘A’: case ‘B’: cout << “Good Work”; break; case ‘C’: cout << “Average Work”; break; case ‘D’: case ‘F’: cout << “Poor Work”; numberInTrouble++; break; }

  6. switch vs if if ( grade == ‘A’ || grade == ‘B’ ) cout << “Good Work”; else if ( grade == ‘C’ ) cout << “Average Work”; else if ( grade == ‘D’ || grade == ‘F’ ) { cout << “Poor Work”; numberInTrouble++; }

  7. Do-While Loop • The do while loop is almost the same as the while loop • The difference is the loop condition is tested at the bottom of the loop • This behavior ensures that the do-while loop is always executed at least once.

  8. Example do { cout << “Enter your age: “; cin >> age; if ( age <= 0 ) cout << “Your age must be positive.\n”; } while ( age <= 0 );

  9. For Loop • The For statement is designed to simply the writing of count controlled loops • You simply take all the pieces you had in a while loop and stick them in the slots in the for loop

  10. Example count = 1; while ( count <= n ) { cout << count << ‘\n’; count++; } for ( count = 1; count <= n; count ++ ) cout << count << ‘\n’;

  11. Break and Continue • The break statement can be used with any loop and switch statements. • If causes the inner most control structure to be exited. • Break can be used to simplify logic, but should be used only as a last resort, not a daily occurrence. • Continue can be used only with loops • It causes the loop to skip to the bottom of the loop iteration and begin a new loop iteration.

  12. Loop Choices • If the loop is a count-controlled loop, the For loop is a natural choice. • If the loop is an event-controlled loop whose body should execute at least once, choose a do-while loop. • If the loop is an event-controlled loop and nothing is known about the first execution, use a while loop • When in doubt, use a while loop. • Pages 453-454.

More Related