100 likes | 216 Vues
This guide covers essential control structures in programming, highlighting the Switch statement, a selection control structure allowing multiple branches based on an expression value. It compares it to nested if statements. Additionally, we discuss the Do-While loop, which ensures the loop body executes at least once, and the For loop, ideal for count-controlled iterations. The Break statement provides an immediate exit from loops, while the Continue statement skips the current iteration without terminating the loop. Enhance your coding skills with these fundamental concepts!
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.