Understanding Control Structures: do/while Loops, Break, Continue, and Switch Statements
150 likes | 271 Vues
This overview discusses key control structures in programming, specifically the do/while loop, break, continue, and switch statements. The do/while loop ensures code execution at least once, while break and continue statements control the loop's execution flow. Examples illustrate how to implement these structures in practical scenarios. The switch statement offers a clean way to handle multiple conditional paths based on a single variable, including case fall-through concepts and common errors to avoid. Perfect for beginners wanting to grasp looping and branching logic in programming!
Understanding Control Structures: do/while Loops, Break, Continue, and Switch Statements
E N D
Presentation Transcript
Overview • do/while loop • break statement • continue statement • switch statement
do/while loop do { statement; } while (condition); Step1: Execute statements inside braces Step2: Evaluate condition. Step3: If condition is true repeat Step1 through 3, otherwise continue execution at next statement following the do/while statement Usually use { } even when not necessary Useful when you want the statements to execute at least once. i.e reading input.
do while example do { cout << “Enter a number between 1 and 10: “; cin >> number; } while (number <= 0 || number > 10);
do/while example What will the output of the following program sequence be? int j = 4; do { j = j - 2; cout << “Hi”; } while (++j > 0); Walk through this example showing how j changes.
Loop Exercises 1) How many times does the statement inside the do/while body execute? What gets displayed? int i = 0, x = 2; do { x = x + i; } while (++ i < 3); cout << “x = “ << x << endl; 2) How many times does the cout statement execute? for (int i = 1; i <= 4; i++) { for (int j = 1; j <= 3; j++) { cout << “Hello” << endl; } }
continue statement skip the rest of this iteration and continue with the next iteration of the loop int x = 1; while ( x <= 10) { if (5 == x) { x++; continue; } cout << x << endl; x++; } condition (x <= 10) is next statement executed after the continue statement.
continue statement • for (int x = 1; x <= 10; x++) { • if (5 == x) • continue; • cout << x << endl; • } • next statements executed after continue are • x ++; • x <= 10;
break statement • Breaks out of the loop. Next statement executed after a • break statement is the statement immediately following the loop. • for (int x = 1; x <= 10; x++) { • if (5 == x) • break; • cout << x << endl; • } • Next statement executed after the break statement is the • statement immediately following the for loop.
else if example if (‘A’ == lettergrade) gpa = 4.0; else if (‘B’ == lettergrade) gpa = 3.0; else if (‘C’ == lettergrade) gpa = 2.0; else if (‘D’ == lettergrade) gpa = 1.0; else if (‘F’ == lettergrade) gpa = 0.0; else cout << “Illegal letter grade” << endl;
switch statement switch (lettergrade) { case ‘A’: gpa = 4.0; break; case ‘B’: gpa = 3.0; break; case ‘C’: gpa = 2.0; break; case ‘D’: gpa = 1.0; break; case ‘F’: gpa = 1.0; break; default: cout << “Illegal letter grade” << endl; }
switch statement syntax switch (controlling expression) { case label1: statement(s); break; case label2: statement(s); break; . . . case labeln: statement(s); break; default: statement(s); }
switch statement rules • controlling expression must be of type int or char • labels must be constants that match the type of the controlling expression • default statement can be anywhere. More intuitive at bottom. • can have multiple case labels sharing the same code case ‘A’: case ‘a’ : statements; break;
else if that cannot be implemented as a switch int score; cout << “Enter score: “; cin >> score; cout << “Your grade is “; if (score < 68) cout << “NR” << endl; else if (score < 78) cout << “C” << endl; else if (score < 88) cout << “B” << endl; else cout << “A” << endl;
common programming errors • Forgetting the break statement. Statements for next case get executed by mistake. • Forgetting the space between the word case and the label. Will not be flagged by compiler but won’t execute the way you intend.
Grandma’s Attic for (char letter = 'a'; letter <= ‘i'; letter++) { cout << "I'm going to Grandma's attic and I'm going to get "; switch (letter) { case 'i': cout << "an igloo, "; case 'h': cout << "a hat, "; case 'g': cout << "a goat, "; case 'f': cout << "a fox, "; case 'e': cout << "an elephant, "; case 'd': cout << "a dinosaur, "; case 'c': cout << "a cookie, "; case 'b': cout << "a ball and "; case 'a': cout << "an apple" << endl; default: break; } }