100 likes | 209 Views
Additional Control Structures. Robert reaves. Switch Statement. Switch( IntegralOrEnumExpression ) { switchLabel …Statement . . } IntegralOrEnumExpression is of type char, short, int , long, bool , or enum . Switch Statement.
E N D
Additional Control Structures Robert reaves
Switch Statement • Switch(IntegralOrEnumExpression) { • switchLabel…Statement • . • . • } • IntegralOrEnumExpressionis of type char, short, int, long, bool, or enum.
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;
Do-While Statement • do { • statement • } while( expression );
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.
For Statement • For (InitStatement Expression1; Expression2) • Statement
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.
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.
Continue Statement • Continue statement terminates the current loop iteration, but not the entire loop. • Valid only within loops.