Flow Chart of Do while loop in C
This is a flowchart that represents the process of executing the Do while loop in the C programming language
Generally, as we know there are three main components of Do While loop:
1. The initialization statement: This statement initialize the C variables.
2. The update statement: This statement this helps in altering the value for further execution.
3. The termination condition: This statement checks the loop for further execution.
Here, you can see that declaration and initialization of variable by value with the help of assignment operator is done before the do while loop. Then, body of do while loop execute first.
In the next step, we have to use the increment or decrement statement that will be executed after the execution of statements of the body.
The last component of loop will execute a test expression that will certainly help in dismissing the do while loop or from preventing it to run toward infinity as per the requirement. If the termination condition or test expression is true then the loop will go to execute for next time and again check whether the value returned by the termination condition is true or false. Loop will continue to execute until the condition becomes false. As the value is found to be false then the control will come out of the loop and the loop will completely dismissed.
Example:
Here is program in c that will print numbers from 11 to 20 with the help of do while loop:
#include <stdio.h>
int main()
{
int a=11; /* declaring a variable ‘a’ */
do
{
printf ("%d ", a);
/* printing final value of variable ‘a’
}
While (a<=20)
printf ("%d ", a);
/* printing final value of variable ‘a’ */
++a;
return 0;
}
Explanation:
Initially we declared a variable ‘a’. Then, we have for loop syntax that contains 3 main components which are as follows:
- a=11 (Initialization statement)
- ++a (increment or decrement statement)
- a<=20 (termination condition)
The value of ‘a’ is initialized with 11 before the do while loop. Then, body of the loop will execute. After that, the modification statement helps in altering the value of ‘a’ so that further evaluations can be done. This can be pre-increment, post-increment, pre-decrement, or post-decrement.
And, the termination condition will check that the value of ‘a’ is smaller than 20 or not. When the value of ‘a’ goes higher than 20 then the termination condition will terminate the for loop.
Two More Examples of Do while Loop:
Example 1:
#include <stdio.h>
int main()
{
int a=11; /* declaring a variable ‘a’ */
do
{
printf ("%d ", a);
/* printing final value of variable ‘a’
}
While (a>=2)
printf ("%d ", a);
/* printing final value of variable ‘a’ */
}
--a;
return 0;
}
Example 2:
#include <stdio.h>
int main() {
double number, sum = 0;
/* declaring various variables in distinct data types and initializing their values */
do {
printf("Enter a number: ");
/* printf statement to let the user to provide input */
scanf("%lf", &number);
/* scanf statement to assign the provided value from user to the given variable */
sum += number;
}
while (number != 0.0);
printf("Sum = %.2lf",sum);
/* printf statement to display the result */
return 0;
}