(Latest Revision: Sun Nov 4 15:54:53 PST 2007 )
====================== LOOPS - Section 2.3 ====================== Examples: #1: WHILE-LOOP CODE: /* initialization - not part of the loop - just getting N ready for use in the loop. */ N = 5 ; /* The code in between the lines below is considered one statement. Execution begins on the line starting with the keyword [while]. The expression inside parentheses is checked (in this case N > 0). If true, then the loop continues: execution next proceeds to the statements inside the braces following the keyword [while], and then back to the line starting with the keyword [while] - as before. If false, then execution of the loop is terminated: execution skips the statements inside braces and proceeds to whatever comes after the loop (in this case the cout << endl << endl statement). */ /* ------------------------------------------ */ while (N > 0) { cout << "WHILE-LOOP! " ; N = N - 1 ; } /* ------------------------------------------ */ cout << endl << endl ; WHAT IS THE OUTPUT? WHILE-LOOP! WHILE-LOOP! WHILE-LOOP! WHILE-LOOP! WHILE-LOOP! Comment: Notice that when the loop executes it is possible that the statements inside the braces may not execute even once. Notice there is no semi-colon after while (N > 0) - very important there NOT be a semi-colon there. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #2: DO-LOOP CODE: /* initialization - not part of the loop - just getting N ready for use in the loop. */ N = 4 ; /* The code in between the lines below is considered one statement. Execution begins with the first statement inside the { } braces. The statements inside the braces are executed in the normal order determined by C++ syntax. After execution reaches the end of the set of statements inside the braces, the expression after the keyword [while] is evaluated (in this case N > 0). If true, then the loop continues: execution next proceeds to the statements inside the braces following the keyword [do], and then back to the line starting with the keyword [while] - as before. If false, then execution of the loop is terminated: execution proceeds to whatever comes after the loop (in this case the cout << endl << endl statement). */ /* ------------------------------------------ */ do { cout << "DO-LOOP! " ; N = N - 1 ; } while (N > 0) ; /* ------------------------------------------ */ cout << endl << endl ; WHAT IS THE OUTPUT? DO-LOOP! DO-LOOP! DO-LOOP! DO-LOOP! Comment: Notice that when the loop executes, the statements inside the braces must execute at least once. Notice there IS a semi-colon after while (N > 0) - very important that the semi-colon be there. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #3: FOR-LOOP CODE: FORMAT: for ( Initialization_Action ; Boolean_Expression - condition for continuing ; Update_Action ) single- or compound-statement /* The code in between the lines below is considered one statement. The for-loop executes like this: 1. Do the action called for by the first item in the parenthesized list (the one following the keyword [for] (in this case N = 0). 2. Next the value of the second item in that list is checked to determine whether true or false (in this case N < 3). 3. If true, then execute the statements inside the braces, execute the action(s) called for by the third item in the parenthesized list (in this case N++), and then go back to step 2. 4. else the value is false and execution of the loop terminates: execution skips the statements inside braces and proceeds to whatever comes after the loop (in this case the cout << endl << endl statement). */ /* ------------------------------------------ */ for (N = 0; N < 3; N++) { cout << "FOR-LOOP! " ; } /* ------------------------------------------ */ cout << endl << endl ; WHAT IS THE OUTPUT? FOR-LOOP! FOR-LOOP! FOR-LOOP! Comment: Some people consider the FOR-LOOP to be a way to do "WHILE-LOOPING," but with a simplified syntax. On the other hand, some people prefer to use WHILE-LOOPS. Basically, a FOR-LOOP can do anything a WHILE-LOOP can do, and vice-versa. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Exercise #24 on page 69 of Savitch: What is the output of this code? int x = 10 ; do { cout << x << endl ; x = x - 3 ; } while (x > 0) ; Here's how to carefully "desk check" Do a time line: x == 10 go to top of statement list output 10 x == 7 check x > 0 -- answer: true return to top of statement list output 7 x == 4 check x > 0 -- answer: true return to top of statement list output 4 x == 1 check x > 0 -- answer: true return to top of statement list output 1 x == -2 check x > 0 -- answer: false do not return to top of statement list - go on to next statement below. Summary of outputs: 10 7 4 1 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ When you start experimenting with writing loops in your programs, be ready for the possibility of an "infinite loop". Savitch's Example: x = 2; while (x != 12) { cout << x << endl ; x = x + 2 ; } The code above correctly prints out the first five positive even integers: 2, 4, 6, 8, 10. Suppose the programmer decides to modify the code to print out the first five positive odd integers, and makes this change: x = 1; // different initial value for x while (x != 12) { cout << x << endl ; x = x + 2 ; } Maybe this looks like it ought to work OK. We can compile and execute a program containing this code. It would be natural to do that as a way to test it. However if we do that the code will "run away". There is nothing built into the loop to make it stop. It is an example of what we call an "infinite loop". When you run this you will just see an unending stream of odd numbers. Another example might be this: x = 2; while (x != 12) ; // Error - the semi-colon on this line { cout << x << endl ; x = x + 2 ; } This is also an "infinite loop." This one doesn't do anything. So if you run a program with this loop inside, you will just see the cursor blinking off and on seemingly without end. If you make a mistake and execute a program containing an infinite loop, what should you do? The answer is: abort the program by doing a control-C. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ The commands below are seldom used (and probably should never be used in loops) break: ends the nearest enclosing switch or loop statement continue: ends the current iteration of the nearest enclosing loop. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ==================================== Suggested Exercise ==================================== Design the loop required for problem #1 on page 87 - For, While, and Do Loop versions