1 / 9

Additional Control Structures

Learn about switch, do-while, for, break, and continue statements in programming. Understand their syntax, usage, and benefits in control flow structures.

xiu
Download Presentation

Additional Control Structures

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. Additional Control Structures Robert reaves

  2. Switch Statement • Switch(IntegralOrEnumExpression) { • switchLabel…Statement • . • . • } • IntegralOrEnumExpressionis of type char, short, int, long, bool, or enum.

  3. Switch Statement • A selection control structure that allows us to list any number of branches. • Similar to the nested if statement. • Switch expression – an expression whose value is matched with a label attached to a branch. • switch(letter) { // letter is the switch expression. • case ‘X’: • statement1; • break • case ‘L’: • statement2; • break;

  4. Do-While Statement • do { • statement • } while( expression );

  5. Do-while Statement • Is a loop control structure in which the loop condition is tested at the end (bottom) of the loop. • Guarantees the loop body to be executed at least once.

  6. For Statement • For (InitStatement Expression1; Expression2) • Statement

  7. For Statement • Designed to simplify the writing of count-controlled loops. • Ex: • for(int x = 0; x < n; x++) { • cout << x << endl; • } • Merely a compact notation for a While loop. Complete essentially translates them the same.

  8. Break Statement • Break statement causes an immediate exit from the innermost Switch, While, do-while, or for statement in which it appears. • Use break sparingly; overuse can lead to confusing code.

  9. Continue Statement • Continue statement terminates the current loop iteration, but not the entire loop. • Valid only within loops.

More Related