Skip to main content
Logo image

Section 13.2 for vs. while Loops

Though for- and while-loops can accomplish the same tasks, both have certain benefits and drawbacks. Here, we compare the syntaxes of both loops and discuss situations and scenarios in which one loop is preferable over the other.
for-loop syntax:
for (initialization; loop run condition; update) {
    statements...;
}
At the very start of the loop (before any kind of repetition is entered) the initialization statement is executed. This happens exactly once. At the start of every repetition the loop condition is checked. If it evaluates to true then the actual loop statements are executed once. Afterwards, the update statement is executed and next, the loop run condition is checked again. If still true, the loop statements are executed again, etc.
Example:
for (i=10; i>5; i--) {
    printf("i = %d\n", i); 
}
In this example, i is initialized with the value 10 right before the start of the loop. Next, the logic statement i>5 is evaluated, and since 10 is indeed greater than 5, the loop is entered. The printf() statement prints i = 10 to the screen. Next, the update statement i-- is executed, decreasing the value of i to 9. The loop condition is checked (9 is still greater than 5) and so i = 9 is printed to the screen. i is next decreased to 8, loop condition checked, etc. This continues on until i = 6 is printed to the screen. When i is next decreased to 5, the check of the loop condition evaluates to false since 5 is not greater than 5. The loop terminates with the loop counter having the value 5.
while-loop syntax:
while (condition) { 
    statements...; 
}
At the start of every run through the loop, the condition is checked and only if it evaluates to true are the statements in the loop body executed. It is the programmer’s job to build in an update condition into these statements so that eventually the loop condition will evaluate to false, causing the loop to terminate. It is really easy to forget to do this, leading to a never-ending loop...
Example:
i=10; 
while (i>5) {
    printf("i = %d\n", i);
    i--; 
}
This loop generates the exact same output as the above for loop. Notice that you have to explicitly initialize i on your own before the loop and program the update condition i--; as part of the loop body.
In general:
  • Use a for-loop when you have a known number of iterations.
  • Use a while-loop when you have an unspecified number of iterations.

Investigate 13.1.

Would a for- or while-loop be more efficient to implement in the following scenario?
In your program, you want to use a loop to continuously get input from the user, and you want to continue to take in user input until a certain integer is entered.
Clearly, the following would be difficult to accomplish with a for-loop. We are asking the user to enter a positive number, and in case they accidentally put in a negative number, have them repeat the input.

admin.....open in new window

In the previous example, we had to type the scanf() statement twice: once to read the user input and then one more time in case they entered something negative by accident. This is not the most elegant code (though perfectly acceptable). So there is a third kind of loop that helps in such a case, the do-while-loop:

admin.....open in new window

Unlike our previous two types of loops, a do-while loop is guaranteed to execute its loop body at least once since it doesn’t get around to checking the loop condition until after execution of the loop body.

Investigate 13.2.

What differences between while-loops and do-while-loops do you notice? Are there any similarities in their syntaxes?