Articles

Difference between While and Do While loop in Tabular Form

Share

In this article we’ll be discussing the difference between while and do while loop and how they work.

Control statements provide an ability to execute a particular set of statements in a controlled manner. Basically, these statements are powerful statements that can mange the flow of a program.

Every programming language supports a basic form of Iteration statements that are known as loops, for example, While, Do-While and For loop.

 

Iteration statements are categorised as the control statements. It allows us to iterate through a piece of code repeatedly.

The goal of all looping statements is to run a particular block of statements frequently until a specific condition is met. However, Do-While and While loop follows a completely different approach to looping around a statement. Both types of loops are different from each other when it comes to their order of execution and condition evaluation.

What is a loop?

A loop can be defined as a control statement that enables repetitive execution of a block of code until a given condition turns out to be false. It provides capability to write a block of code and execute it repeatedly for specific number of times. Suppose, if a block of code needs to be executed repeatedly, in such case, we must use loops, instead of writing the same code multiple times.

Normally a looping statement comprises of following components:

  • Control or counter variable initialization
  • Condition evaluation
  • Loop body execution
  • Counter variable updation

There are primarily three types of loops  :
1) while loop
2) do while loop
3) for loop

The main difference between While and Do-While loop is that one evaluates condition first and then executes the loop body, whereas, other one executes the loop body first and then checks for the condition.

Difference between While and Do While Loop in Tabular form

BasisWhile LoopDo-While Loop
DefinitionIn this, the given condition is evaluated first, and then loop body is executedIn this, the given loop body is executed first and then after the given condition is checked
Loop TypeIt is an entry-controlled loopIt is an exit-controlled loop
Loop ExecutionThe loop body would be executed, only if the given condition is trueThe loop body would be executed at least once, even if the given condition is evaluated as false
Counter Variable IntializationIt allows initialization of counter variable before entering loop bodyIt allows initialization of counter variable before and after entering loop body
SemicolonNo semicolon is used as a part of syntax,
while(condition)
Semi-colon is used as a part of syntax,

while(condition);
UsageIt is used when condition evaluation is required to be evaluated first, before executing loop bodyDo-While is used when one needs to enter into the loop body before evaluating condition. For example, Menu driven programs
SyntaxSyntax:

while( condition )
{
// loop body
}
do
{
// loop body
}while( condition );

While Loop

While Loop can be defined as the loop where the condition is evaluated first and then after the loop body is executed. It is an entry controlled loop.

Syntax:

while(condition)
{
// body
}

How while loop works?

  • Counter or control variable initialization
  • Then, condition would be checked
  • If it is evaluated as true, then
  • loop body would be executed
  • Otherwise, the loop gets terminated

A Program example for While loop:

Example 1. C program to demonstrate working of while loop

Program

#include <stdio.h>

int main ()
{

  int i = 1;
  while (i < 5)
    {
      printf ("Print :: %d \n", i);
      i++;
    }
  return 0;
}

Output:

Print :: 1 
Print :: 2 
Print :: 3 
Print :: 4 

Explanation:

In the above example, we are simply initializing counter variable i with 1. And iterating using while loop until the condition  having i is less than 5 becomes false. In loop body, we are printing a message and incrementing counter variable on very next line.

Example 2. C program to show the approach difference of while loop with do-while

#include<stdio.h>
void main()
{
int i=10;
while(i<10)
{
printf("I will not be executed as it is entry controlled loop");
i++;
}
getch();
}

Explanation:

Since, condition would be evaluated as false at very first place , no output would be printed. In this, counter variable ‘i’ was holding value 10. And at the entry point of while loop, when condition got evaluated for ‘i< 10’, it turned out to be false. As a result, the loop terminated.

Do-While Loop

Completely opposite to While loop, Do-While is a looping statement in which condition is checked after the execution of the loop body. It is a type of exit controlled loop.

Syntax:

do
{
// loop body

}whie(condition);

How do-While works?

  • Counter or control variable initialization
  • Loop body gets executed first and then-after the condition is evaluated
  • If the condition is evaluated as true, then loop would proceed to next iteration
  • Otherwise, it gets terminated

A  Program example of Do-While loop

Example 1. C program to demonstrate working of do while loop

Program:

#include <stdio.h>

int main ()
{
  int i = 1;
  do
    {
      printf ("Print :: %d \n", i);
      i++;
    }while (i < 5);
  return 0;
}

Output:

Print :: 1 
Print :: 2 
Print :: 3 
Print :: 4 

Explanation:

In the above example, we are initializing counter variable i with 1. And iterating using do while loop until i is less than 5. In loop body, we are printing a message and incrementing counter variable on very next line. Lastly, we’re checking the condition and iterate accordingly.

Example 2. C program to show the approach difference of do-while loop with while

#include<stdio.h>
void main()
{
  int i=10;
  do
  {
  printf("I will be executed at once as it is exit controlled loop");
  i++;
  }while(i<10);
  getch();
}

Explanation:

Print statement would be executed at once, even though the condition is false. In this, counter variable ‘i’ was 10. In this, the loop body got executed first which resulted in print statement execution. And lastly, condition got evaluated for ‘i< 10’, it turned out to be false. As a result, the loop terminated.

Exit Controlled and Entry Controlled Loop

A loop that executes the loop body first and then checks for a condition is known as an exit controlled loop. For example Do-While loop.

On the other hand, a loop that checks for condition first and run the loop body can be categorized as entry controlled loop. For example, While loop, and For loop.

This post was last modified on March 13, 2024

Sandeep Verma

Published by
Sandeep Verma
Tags: control statements Difference between While and Do-While loop do while loop Iteration Statements Programming While and Do-While loop while loop