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.
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:
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.
Basis | While Loop | Do-While Loop |
---|---|---|
Definition | In this, the given condition is evaluated first, and then loop body is executed | In this, the given loop body is executed first and then after the given condition is checked |
Loop Type | It is an entry-controlled loop | It is an exit-controlled loop |
Loop Execution | The loop body would be executed, only if the given condition is true | The loop body would be executed at least once, even if the given condition is evaluated as false |
Counter Variable Intialization | It allows initialization of counter variable before entering loop body | It allows initialization of counter variable before and after entering loop body |
Semicolon | No semicolon is used as a part of syntax, while(condition) | Semi-colon is used as a part of syntax, while(condition); |
Usage | It is used when condition evaluation is required to be evaluated first, before executing loop body | Do-While is used when one needs to enter into the loop body before evaluating condition. For example, Menu driven programs |
Syntax | Syntax: while( condition ) { // loop body } | do { // loop body }while( condition ); |
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 }
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.
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);
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.
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