![]() |
VOOZH | about |
C do...while loop is a type of loop that executes a code block until the given condition is satisfied. Unlike the while loop, which checks the condition before executing the loop, the do...while loop checks the condition after executing the code block, ensuring that the code inside the loop is executed at least once, even if the condition is false from the start.
Geeks Geeks Geeks
Explanation: The do...while loop in this C program prints "Geeks" three times by executing the loop body at least once and continuing until the condition i < 3 becomes false.
where,
Let's understand the working of do while loop using the below flowchart.
The initialization and updation is not a part of the do...while loop syntax. We have to do that explicitly before and in the loop respectively.
The below programs demonstrate how to use the do while loops in C programs in different situations:
This is loop body.
Explanation: As we can see, the body of the loop is executed even if the condition was false from the start.
We can also nest one do...while loop into another loop (called nested loops). This helps in printing multidimensional structures such as matrices.
0 1 2 3 4 5 6 7 8
The following table lists the important differences between the while and do...while Loop .
while Loop | do...while Loop |
|---|---|
| The test condition is checked before the loop body is executed. | The test condition is checked after executing the body. |
| When the condition is false, the body is not executed not even once. | The body of the do...while loop is executed at least once even when the condition is false. |
| It is a type of pre-tested or entry-controlled loop. | It is a type of post-tested or exit-controlled loop. |
| Semicolon is not required. | Semicolon is required at the end. |