Chapter 7 Additional Control Structures
2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response has been input // && response == ‘y’ or ‘n’ { do { cin >> response; // Skips leading whitespace if ((response != ‘y’) && (response != ‘n’)) cout << “Please type y or n : “; } while ((response != ‘y’) && (response != ‘n’)); } Example of Do-While 2
3 3 Do-While Loop vs. While Loop l POST-TEST loop (exit-condition) l The looping condition is tested after executing the loop body l Loop body is always executed at least once l PRE-TEST loop (entry-condition) l The looping condition is tested before executing the loop body l Loop body may not be executed at all
4 4 Do-While Loop When the expression is tested and found to be false, the loop is exited and control passes to the statement that follows the Do-while statement Statement Expression DO WHILE FALSE TRUE
5 5 For Loop SYNTAX for (initialization; test expression; update) { Zero or more statements to repeat }
6 6 For loop contains l An initialization l An expression to test for continuing l An update to execute after each iteration of the body
7 7 Example of For Loop int num; for (num = 1; num <= 3; num++) { cout << num << “Potato” << endl; }
8 8 Example of Repetition num int num; for (num = 1; num <= 3; num++) cout << num << “Potato” << endl; OUTPUT ?
9 9 Example of Repetition int num; for (num = 1; num <= 3; num++) cout << num << “Potato” << endl; num OUTPUT 1
10 Example of Repetition num OUTPUT 1 int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl; true
11 Example of Repetition num int num; for (num = 1; num <= 3; num++) cout << num << “Potato” << endl; OUTPUT 1 1Potato
12 Example of Repetition num OUTPUT 2 int num; for (num = 1; num <= 3; num++) cout << num << “Potato” << endl; 1Potato
13 Example of Repetition num OUTPUT 2 true 1Potato int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl;
14 Example of Repetition num int num; for (num = 1; num <= 3; num++) cout << num << “Potato” << endl; OUTPUT 2 1Potato 2Potato
15 Example of Repetition num OUTPUT 3 int num; for (num = 1; num <= 3; num++) cout << num << “Potato” << endl; 1Potato 2Potato
16 Example of Repetition num OUTPUT 3 true 1Potato 2Potato int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl;
17 Example of Repetition num int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl; OUTPUT 3 1Potato 2Potato 3Potato
18 Example of Repetition num OUTPUT 4 int num; for (num = 1; num <= 3; num++) cout << num << “Potato” << endl; 1Potato 2Potato 3Potato
19 Example of Repetition num OUTPUT 4 false 1Potato 2Potato 3Potato int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl;
20 Example of Repetition num When the loop control condition is evaluated and has value false, the loop is said to be “satisfied” and control passes to the statement following the For statement 4 false int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl;
21 Output The output was 1Potato 2Potato 3Potato
22 int count; for (count = 4; count > 0; count--) { cout << count << endl; } cout << “Done” << endl; Count-controlled Loop OUTPUT: Done
23 What is output? int count; for (count = 0; count < 10; count++) { cout << “*”; }
24 Answer ********** The 10 asterisks are all on one line. Why?
25 What output from this loop? int count; for (count = 0; count < 10; count++); { cout << “*”; }
26 l No output from the for loop! Why? l The semicolon after the () means that the body statement is a null statement l In general, the body of the For loop is whatever statement immediately follows the () l That statement can be a single statement, a block, or a null statement Actually, the code outputs one * after the loop completes counting to 10 Answer
27 Several Statements in Body Block const int MONTHS = 12; int count; float bill; float sum = 0.0; for (count = 1; count <= MONTHS; count++) { cout << “Enter bill: “; cin >> bill; sum = sum + bill; } cout << “Your total bill is : “ << sum << endl; 27
28 Break Statement l The Break statement can be used with Switch or any of the 3 looping structures l It causes an immediate exit from the Switch, While, Do-While, or For statement in which it appears l If the Break statement is inside nested structures, control exits only the innermost structure containing it
29 Guidelines for Chooling Looping Statement l For a simple count-controlled loop, use the For statement l For an event-controlled loop whose body always executes once, use of Do-While statement l For an event-controlled loop about which nothing is known, use a While statement l When in doubt, use a While statement
30 Continue Statement l The Continue statement is valid only within loops l It terminates the current loop iteration, but not the entire loop l In a For or While, Continue causes the rest of the body of the statement to be skipped; in a For statement, the update is done l In a Do-While, the exit condition is tested, and if true, the next loop iteration is begun