do-while loop in c
do-while loop in c, first executes statements, then check the test condition. It means do while loop executes statements at least once, if test condition is false. It is an exit control loop.
Syntax
initialization;
do{
// statements
// iteration;
}while(condition);
initialization | The start value from where the do while loop starts. |
condition | It specifies the number of times do while loop will repeat statements. |
iteration | It continue the loop, by increasing value to continue the loop cycle. |
Example
#include<stdio.h>
void main()
{
int i = 0; // initialization
do {
printf("%d ", i);
i++; // iteration;
} while (i < 5); //condition
}
Explanation
- int i = 0; is initialization, the value from where do while loop starts.
- i++; is iteration. It continues do while loop cycle.
- while (i < 5); It is test condition, It checks the condition, and executes the loop.