What Are The Difference Between For While And Do While Loop?

In this post, I will discuss with you about what is the difference between for while and do while loop. Also, Compare these 3 loops with examples. In c programming, we need these 3 types of loops while we are solving problems. So, we must know the comparison among different types of loops in c. So, that we can use the best loop needed for a particular problem. Also, if we know the characteristics of these loop statements we can decide which loop can be used for which type of situation.
 
The loop statement in c is a statement using which we can perform a specific task under a given condition continuously and repeatedly until the condition becomes false.
In C programming basically, we need 3 loop statements. They are:
  1. For loop.
  2. While loop.
  3. Do…while loop

 

Among them For loop and while loop statements are Entry controlled loop and do…while loop is Exit controlled loop. In my previous post, I have discussed all the loop statements in C in detail with examples. So, in this post, I am not going to discuss these loop statements in detail. If you haven’t read that article Read this article loop statement in c.
I will give you a brief discussion about these 3 loops in this post.
 
 
 
 
 
What Are The Difference Between For While And Do While Loop.

 

 
 
 
 

For Loop:

 
As you know for loop is an entry control loop, in this loop at first conditions are checked then the operation statement is executed.
 
Syntax:
 
For(initialization; Condition; Increment or decrement)
{
Operation;
}
 
 

While Loop:

 
While loop is also an entry controlled loop similar to for loop.
 
Syntax:
 
Initialization;
while(condition)
{
    Operation;
    Increment or decrement;
}
 
 
 
 
 
 
 
 
 
 

Do…While Loop:

 
Do…While is an exit controlled loop. In this loop at the first operation, the statement is performed once, and then the condition is checked.
 
Syntax:
 
Initialization;
do{
    Operation statement;
    Increment or decrement;
}
while(condition);
 
 
 
 
 

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);

}

 

Input: 0
 
Output: The operation statement executed for 0 times
 
Input: 2
 
Output: The operation statement executed for 2 times
Code for while loop statement:
#include<stdio.h>
int main()
{
    int i,j,k,p,n=0;
    scanf(“%d”,&k);
    i=1;
    while(i<=k)
    {
        n++;
        i++;
    }
    printf(“The operation statement executed for %d timesn”,n);
}
Input: 0
 
Output: The operation statement executed for 0 times
 
Input: 2
 
Output: The operation statement executed for 2 times
 
Code for do…while loop statement:
#include<stdio.h>
int main()
{
    int i,j,k,p,n=0;
    scanf(“%d”,&k);
    i=1;
    do{
        ++i;
        n++;
    }while(i<=k);
    printf(“The operation statement executed for %d timesn”,n);
}
 
 
Input: 0
 
Output: The operation statement executed for 1 times
 
Input: 2
 
Output: The operation statement executed for 2 times
I hope now your concern is clear about the difference between for while and do while loop in c. If you still have any further questions or if you don’t understand the above codes comment below. Thank you.

 

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *