Do While loop in C programming executes the code block lines at least once, even if the condition Fails. It tests the condition at the loop end.
This scenario explains the situation, where we can use the Do While loop in C programming. The While loop discussed in our previous article tests the condition before the compiler enters the code block. If the condition result is True, only code inside the loop executes. Otherwise, it won’t run at least once. In some circumstances, it is required to execute some operations (perform some statements) first and then check the condition.
C Programming Do While loop Syntax
The syntax of the Do While Loop in this Programming is as shown below:
do { statement 1; statement 2; …………. statement n; } While (condition);
First, it executes the lines inside the loop, and after arriving at the end, the compiler checks the while condition. If the condition returns True, it recurs the process; the Do While Loop terminates if it fails.
NOTE: Put a semi-colon in the Do While after the While condition.
Do While Flow Chart
Flow chart sequence of a Do while loop in this C Programming language is as shown below.
- Variable initialization, and then it enters the iteration.
- Execute/Run a group of statements within the Programming.
- Next, use the Increment and Decrement Operator inside the loop to increment or decrements the values.
- Next, it checks the condition. If the condition output is True, the code inside the Do while loop executes again. The process will last until the condition fails.
- If it is False, the compiler exits from it.
Do While Loop in C Programming Example
This program helps you to understand the Do While Loop.
#include <stdio.h> int main() { int number, total=0; printf("\n Please Enter any integer below 10 \n"); scanf("%d", &number); do { total = total + number; printf(" Number = %d\n", number); printf(" Total Value is: %d\n", total); number++; }while (number< 10); printf(" Total Value from outside is: %d \n", total); return 0; }
In this program, the User will enter any value below 10, and the total variable will be initialized to 0.
- User entered value will assign to the number variable, and then the number is added to the total.
- In the next line, we used the ++ operator to increment the number value.
- After this line, the value in a Number variable tests against the while condition. If the condition results true, it repeats the process. Otherwise, it will exit from it.
- In the next line, we used one more printf statement to show that it comes from outside the while loop.
Let us enter a value greater than 10 to see what will happen.
Please Enter any integer below 10
11
Number = 11
Total Value is: 11
Total Value from outside is: 11
If you observe from the above screenshot, we entered value 11, and it still displays the total as 11 because after executing that code, it checked the while condition. It failed, so it exits.
What is the difference between While loop and Do While in C Programming?
Although the Do While and While loop looks similar, their execution differs.
- For a While, the condition is tested at the beginning, and if the condition is True, then only statements in that will execute. It means While executes the code block only if the condition is True.
- For Do While, the condition tests at the end. So Do While loop executes the statements in the code block at least once, even the condition Fails.
I think you will understand it better when you see the example, so let’s write the same program using the While and Do While loop in C.
While Example
In this program, we are declaring integer and then check whether 0 is greater than ten or not. If the condition is True, it will print this statement, “X is Greater Than 10”. There is one more printf statement outside the While, and this statement will execute after the while.
#include <stdio.h> int main() { int x=0; while(x > 10) { printf("\n X is Greater Than 10\n"); } printf("\nStatement Outside of it\n"); return 0; }
Do While Example
We will write the same example using the do while. Remember, although the condition fails, the statement inside the do while loop executed once because of the condition tested after executing the statements.
#include <stdio.h> int main() { int x=0; do { printf("\n X is Greater Than 10 \n"); }while(x > 10); printf("\n Statement Outside of it \n"); return 0; }