Difference between while and do-while: Explained with Examples

What are loops?

A Loop executes the sequence of statements many times until the stated condition becomes false. A loop consists of two parts, a body of a loop and a control statement. The control statement is a combination of some conditions that direct the body of the loop to execute until the specified condition becomes false. The purpose of the loop is to repeat the same code a number of times.

What is While Loop?

A While loop is the most straightforward looping structure. It is an entry-controlled loop. In a while loop, a condition is evaluated before processing a body of the loop. If a condition is true, then and only then the body of a loop is executed.

After the body of a loop is executed, the control again goes back to the beginning, and the condition is checked. If it is true, the same process is executed until the condition becomes false. Once the condition becomes false, the control goes out of the loop.

In a while loop, if the condition is not true, then the body of a loop will not be executed, not even once.

What is a Do-While Loop?

A Do-while loop is similar to the while loop except that the condition is always executed after the body of a loop. It is also called an exit-controlled loop.

In the do-while loop, the body of a loop is always executed at least once. After the body is executed, then it checks the condition. If the condition is true, then it will again execute the body of a loop. Otherwise, the control is transferred out of the loop.

KEY DIFFERENCES:

  • While loop checks the condition first and then executes the statement(s), whereas do while loop will execute the statement(s) at least once, then the condition is checked.
  • While loop is entry controlled loop whereas do while is exit controlled loop.
  • In the while loop, we do not need to add a semicolon at the end of a while condition but we need to add a semicolon at the end of the while condition in the do while loop.
  • While loop statement(s) is executed zero times if the condition is false whereas do while statement is executed at least once.
  • While loop allows initialization of counter variable before starting the body of a loop whereas do while loop allows initialization of counter variable before and after starting the body of a loop.

Syntax of While loop

Here is a syntax of While loop:

while (condition) {
statements;
}

In the while loop, we have to write a condition that needs to be evaluated. The statement inside curly braces indicates the code to be executed.

Syntax Do While Loop

Here is a syntax of Do while loop:

do {
statements
} while (expression);

In the do-while loop, we need to first write the statement inside curly braces, which indicates the code to be executed. After this, we need to mention Java, C, or C++ program expressions that need to be evaluated.

How While Loop Works?

While loop works as follows:

While Loop

Flow Chart Explanation:

Step 1) Start of while loop

Step 2) The test expression or condition is evaluated

Step 3) Next, if the test expression is true, the program executes the body of do-while loop

Step 4) If the test expression is false, the program outside while loop is executed

How Do-While Loop Works?

The Do-while loop works as follows:

Do-While Loop

Flow Chart Explanation:

Step 1) Start the do-while loop

Step 2) The body of do-while loop is executed

Step 3) The test expression or condition is evaluated

Step 4) If the test expression is true, the compiler executes the body of do-while loop

Step 5) Next, if the test expression is false, the compiler executes the statements after the loop body

Step 6) Statements that come after the loop body are executed

While Loop Example in C

Following program illustrates while loop in C programming with an example:

#include<stdio.h>
#include<conio.h>
int main()
{
int num=1;	//initializing the variable with value 1
while(num<=4)	//while loop with condition
{
printf("%d\n",num);
num++;		//incrementing operation
}
return 0;
}

Output:

1
2
3
4

The above program illustrates the use of a while loop. In the above code, we have printed a series of numbers from 1 to 4 using a while loop.

We have initialized a variable called num with value 1. We are going to print from 1 to 4. Hence the variable is initialized with value 1. If we want to print from 0, then assign the value 0 during initialization.

Next, in a while loop, we have provided a condition (num<=4), which means the loop will execute the body until the value of num becomes 4. After that, the loop will be terminated, and control will fall outside the loop.

In the body of a loop, we have a print function to print our number and an increment operator to increment the value per execution of a loop.

An initial value of num is 1, after the execution, it will become 2, and during the next execution, it will become 3. This process will continue until the value becomes 4, and then it will print the series on the console and terminate the loop.

Do While Loop Example in C

The following program is a Do-while loop example to print a table of number 2 in C:

#include<stdio.h>
#include<conio.h>
int main()
{
int num=1;	//initializing the variable with value 1
do	//do-while loop
{
printf("%d\n",2*num);
num++;		//incrementing operation
} while(num<=4);
return 0;
}

Output:

2
4
6
8

In the above example, we have printed a multiplication table of 2 using a do-while loop. First, we have initialized a variable ‘num’ with the value 1. Then we have written a do-while loop.

In a loop, we have a print function that will print the series by multiplying the value of num with 2. After each increment, the value of num will increase by 1, and it will be printed on the screen.

Initially, the value of num is 1. In a body of a loop, the print function will be executed in this way: 2*num where num=1, then 2*1=2.

Hence the value 2 will be printed. This will go on until the value of num becomes 10. Next, the loop will be terminated, and a statement which is immediately after the loop will be executed. In this case, it will return 0.

While vs. Do While

While vs. Do While

Here is an important difference between While and Do While Loop:

While Do While
It checks the condition first and then executes statement(s) This loop will execute the statement(s) at least once, then the condition is checked.
While loop allows initialization of counter variables before starting the body of a loop. Do while loop allows initialization of counter variables before and after starting the body of a loop.
It is an entry controlled loop. It is an exit controlled loop.
We do not need to add a semicolon at the end of a while condition. We need to add a semicolon at the end of the while condition.
In case of a single statement, we do need to add brackets. Brackets are always needed.
In this loop, the condition is mentioned at the starting of the loop. The loop condition is specified after the block is executed.
Statement(s) can be executed zero times if the condition is false. Statement is executed at least once.
Generally while loop is written as:

while (condition) {
Statements; // loop body
}
Generally do while loop is written as:

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

Which One Should We Choose?

When checking a condition, if the first iteration is compulsory, we need to use the while loop. It can also be used if the number of iterations is unknown or uncertain.

Do while loop mainly requires in the case where we have to execute the loop minimum one time. The do-while loop is typically needed in a menu-driven programming language where the final condition is based upon the end-user.