Do While Loop in C Language
The do
while
statement allows the creation of loops in which the body of the loop is executed at least once.
The syntax of the do
while
statement includes two parts: the body of the loop and the control condition. The body is executed before the condition is checked, so a do
while
loop guarantees that the body is always executed at least once.
In this lesson, we will see how to use a do
while
loop, what its syntax is, and how it differs from a regular while
loop.
- Using the
do
while
statement makes it possible to create loops where the condition is evaluated at the end of the loop body; - This ensures that the loop body is always executed at least once.
do
while
Statement
The do
while
statement is closely related to the while
statement. In fact, one could say that the do
statement is essentially a while
loop whose control expression is evaluated after each execution of the loop body.
The form of the do
while
statement is as follows:
do statement while ( expression );
The control expression of the do
while
statement must be enclosed in parentheses. Also, just like with the while
statement, the body of the do
while
statement can be a single statement or a compound statement:
do {
statement_1;
statement_2;
/* ... */
statement_n;
} while ( expression );
When a do
while
statement is executed, the body of the loop is executed first, and only then is the control expression evaluated. If the value of the control expression is nonzero, i.e., the expression is true, the loop body is executed again and the expression is checked once more. The execution of a do
statement ends when the expression evaluates to zero, i.e., false, after the loop body has been executed.
To better clarify the difference between a do
statement and a while
statement, it is useful to revisit the example from the lesson on the while
statement: printing a countdown.
In the while
statement lesson, we implemented a program that prints a countdown starting from 10, like this:
#include <stdio.h>
int main() {
int n = 10;
while (n > 0) {
printf("%d\n", n);
n--;
}
printf("Liftoff!\n");
return 0;
}
If we want to write the same program using the do
while
statement, we can implement it like this:
#include <stdio.h>
int main() {
int n;
n = 10;
do {
printf("%d\n", n);
--n;
} while (n > 0);
printf("Liftoff!\n");
return 0;
}
By examining the behavior of this program, we can see that:
- When the
do
statement is executed, the loop body is executed first. For this reason, the number is printed first, and then the variablen
is decremented. At this point,n
is 9; - The condition
n > 0
is then tested and evaluates to true, sincen
is 9; - The body of the loop is therefore executed a second time;
- The body of the loop will be executed repeatedly until the message
1
is printed and the variablen
is decremented to 0; - At this point, the condition is evaluated and is false, so the loop terminates.
As we can see, a while
loop and a do
while
loop often appear indistinguishable. The real difference lies in the fact that the condition is evaluated after the execution of the loop body. This feature has an important consequence:
The body of a do
while
loop is always executed at least once.
Unlike a while
loop, the body of a do
while
loop is executed before the control expression is evaluated. For this reason, even if the control expression is false before the loop starts, the body of a do
while
loop will always be executed at least once.
This is a crucial difference compared to the while
loop, in which the body may not be executed even once.
A good programming practice is to use braces to enclose the body of a do
while
loop even if they are not strictly necessary. This is because code like the following may mislead an inattentive reader:
do
n = n / 2;
while (n > 0);
At a quick glance, the while
on the third line might be mistaken for the beginning of a while
statement.
Always enclose the body of a do
loop in braces.
To improve code readability, it is recommended to always enclose the body of a do
loop in braces.
So you should avoid writing code like this:
do
statement;
while (condition);
Instead, prefer writing it like this:
do {
statement;
} while (condition);
Summary
In this lesson, we studied the second statement provided by the C language for implementing loops: the do
while
statement.
Unlike the while
statement, with the do
while
statement the control expression that determines the execution of the next iteration is evaluated after the body of the loop itself. This ensures that the loop body is always executed at least once.
In the next lesson, we will study the for
statement, which allows loops to be implemented in a highly flexible manner.