The do...while loop is another kind of loop in C. It is different from the other looping statements. In for loop or while loop, we check for the condition before execution of the statements. But in do... while, we perform execution first and then check for the condition. You will get clear understanding after checking the following program.
First we will know the syntax of the do...while statement.
Syntax:
Thus do...while loop is different from the other loops.
The loop can't be broke unless, the condition fails. However, it could be broken out with the help of break statement or can continue with next iteration with the help of continue.
First we will know the syntax of the do...while statement.
Syntax:
- do
- {
- //Statements
- }while(condition);
Now consider the following example program for understanding this do... while loop clearly.
Example:
- #include<stdio.h>
- main()
- {
- int a=5;
- do
- {
- printf("In loop");
- }while(a!=5);
- return 0;
- }
If you observe the output of the above program, it gives the output to be
In loopHere value of a is 5 and the condition for the loop is true when a is not equal to 5. But the loop executes once and after the first execution, it checks if the condition is satisfied. As the condition is not satisfied, it gets out after the first execution.
Thus do...while loop is different from the other loops.
The loop can't be broke unless, the condition fails. However, it could be broken out with the help of break statement or can continue with next iteration with the help of continue.