The do-while
loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given boolean condition at the end of the block.
Here is the syntax for a do-while
loop in C:
do {
// code block to be executed
} while (condition);
The condition
is a boolean expression that is evaluated at the end of each iteration of the loop. If the condition is true
, the loop will continue to execute. If the condition is false
, the loop will terminate.
Here is an example of a do-while
loop that counts down from 10 to 1:
#include <stdio.h>
int main(void) {
int i = 10;
do {
printf(“%d\n”, i);
i–;
} while (i > 0);
return 0;
}
This program will print the numbers 10 through 1 on separate lines.