What Are The Difference Between For While And Do While Loop?
- For loop.
- While loop.
- Do…while loop
For Loop:
While Loop:
Do…While Loop:
Difference And Comparison Between For, While, And Do…While Loop:
For Loop |
While Loop |
Do…While Loop |
For loop is an entry controlled loop. |
While loop is an entry controlled loop. |
Do…while loop is an exit controlled loop. |
In for loop, variable initialization, conditions, and increment or decrement must be done inside the loop. |
In a while loop, variable initialization must be before the loop, and conditions and increment or decrement are done inside the loop. |
In the do…while loop, Variable initialization must be done outside before the do loop, increment or decrement is done inside the loop, and the condition is checked outside the do loop. |
In for loop, there is no semicolon after the for statement. |
In the while loop, there isn’t a semicolon after the while statement. |
On the do…while loop, there must be a semicolon after the while statement. |
In for loop, the condition is checked first and then the operation is executed if the condition is true. |
In the while loop, the condition is checked first and then the operation is executed if the condition is true. |
In the do while loop, the operation is executed first, and then the condition is checked. If the condition is true operation is executed again. |
In for loop, there is a possibility that the operation statement is executed 0 times. |
In the while loop, there is also a possibility that the operation statement is executed 0 times. |
In do…while loop operation statement must be executed at least once whatever the condition is. |
If there is only one statement then there is no necessity to use the second bracket in for loop. |
In a while loop, the use of a second bracket is compulsory. |
In do…while loop second bracket must be used. |
Syntax: For(initialization; Condition; Increment or decrement) { Statements; } |
Syntax: Initialization; While(condition) { Statement; Increment or decrement; } |
Syntax: Initialization; Do{ Statement; Increment or decrement; }while(condition); |
To know about the conditional statements in c with example click this.
For better understanding follow the codes using these 3 loop statements.
Code for for loop statement:
#include<stdio.h>
int main()
{
int i,j,k,p,n=0;
scanf(“%d”,&k);
for(i=1;i<=k;i++)
n++;
printf(“The operation statement executed for %d timesn”,n);
}