Skip to main content

Section 13.4 The do-while-Loop

The do-while-loop is similar in nature to the while-loop, in the sense that the code within both loops will execute so long as a certain condition is satisfied. The slight difference between the two types of loops is that in a while-loop, the condition is tested before the statements within the loop are executed, whereas in a do-while-loop, the code inside the loop executes once before the condition is ever tested.
Therefore, even if the condition to continue the loop is never met, the do-while-loop will still execute the loop’s statements once, but the while-loop will not execute at all.

Activity 13.3.

Try out the features of the do-while-loop in the code window below. Test what happens if you edit the code so the condition of the loop is NOT met. Change the loop from a do-while-to a while-loop, and see if it produces the same results!

Compile and run the code below in your own C environment.

#include <stdio.h> int main(void) { int i = 1; do { printf("Hello, World!\n"); i++; } while (i < 5); return (0); }
Figure 13.1.