- Here is the example structure of the do while loop.
-
// initialization of the variable used to control the loop
do {
// the statements to execute each time the loop iterates
// included is a specific statement which increments the loop variable
} while (expression);
- Here is the sequence of steps of the while loop.
- The order used is to complete step 1. Then to follow the sequence of step 2, step 3, and step 4.
Once step 4 is finished, return to step 2 and repeat the sequence.
step 1
do {
step 2
step 3
} while (step 4);
- Here is an example code fragment of a do while loop that will print from zero to a user
defined stopping point. Note, the statement body of the loop will execute one time, no matter if
the value of i is greater than or equal to the value of stop.
-
i = 0;
do {
printf("i = %d\n", i);
i++;
} while (i < stop);