do-while loop in C is just as same as while loop, the only difference is that in do-while the condition is always executed after the loop as shown in the syntax below.
Unlike while loop, where the body of the loop is executed only if the condition stated is true but in the do-while loop, the body of the loop is executed at least once. do-while is an exit-control loop that is it checks the condition after the first execution of the block.
do-while Flowchart:
Syntax of do-while Loop.
1 2 3 4 | do { statements.. }while (condition); |
Example of a do-while loop in C program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include <stdio.h> int main() { int num = 1; //execution of do-while loop do { printf("Value of num: %d\n", num); num++; } while (num <= 10); //condition return 0; } |
The output of do-while loop.
Value of num: 1
Value of num: 2
Value of num: 3
Value of num: 4
Value of num: 5
Value of num: 6
Value of num: 7
Value of num: 8
Value of num: 9
Value of num: 10