5.5. do...while Repetition Statement

The do...while repetition statement is similar to the while statement. In the while statement, the loop-continuation condition test occurs at the beginning of the loop before the body of the loop executes. The do...while statement tests the loop-continuation condition after the loop body executes; therefore, the loop body always executes at least once. When a do...while terminates, execution continues with the statement after the while clause. Note that it is not necessary to use braces in the do...while statement if there is only one statement in the body; however, most programmers include the braces to avoid confusion between the while and do...while statements. For example,

while ( condition )

normally is regarded as the header of a while statement. A do...while with no braces around the single statement body appears as

do
   statement
while ( condition );

which can be confusing. You might misinterpret the last line—while( condition );—as a while statement containing as its body an empty statement. Thus, the do...while with one statement often is written as follows to avoid confusion:

do
{
   statement
} while ( condition );

Good Programming Practice 5.5

Always including braces in a do...while statement helps eliminate ambiguity between the while statement and the do...while statement containing one statement.


Figure 5.7 uses a do...while statement to print the numbers 1–10. Upon entering the do...while statement, line 13 outputs counter's value and line 14 increments counter. Then the program evaluates the loop-continuation test at the bottom of the loop (line 15). If the condition is true, the loop continues from the first body statement in the do...while (line 13). If the condition is false, the loop terminates and the program continues with the next statement after the loop (line 17).

Fig. 5.7. do...while repetition statement.
 1   // Fig. 5.7: fig05_07.cpp
 2   // do...while repetition statement.
 3   #include <iostream>
 4   using std::cout;
 5   using std::endl;
 6
 7   int main()
 8   {
 9      int counter = 1; // initialize counter
10
11      do                                           
12      {                                            
13         cout << counter << " "; // display counter
14         counter++; // increment counter           
15      } while ( counter <= 10 ); // end do...while 
16
17      cout << endl; // output a newline
18      return 0; // indicate successful termination
19   } // end main

1 2 3 4 5 6 7 8 9 10


do...while Statement UML Activity Diagram

Figure 5.8 contains the UML activity diagram for the do...while statement. This diagram makes it clear that the loop-continuation condition is not evaluated until after the loop performs the loop-body action states at least once. Compare this activity diagram with that of the while statement (Fig. 4.5). Again, note that (besides an initial state, transition arrows, a merge, a final state and several notes) the diagram contains only action states and a decision. Imagine, again, that you have access to a bin of empty do...while statement UML activity diagrams—as many as you might need to stack and nest with the activity diagrams of other control statements to form a structured implementation of an algorithm. You fill in the action states and decision symbols with action expressions and guard conditions appropriate to the algorithm.

Fig. 5.8. UML activity diagram for the do...while repetition statement of Fig. 5.7.