Difference Between while and do-while Loop (with Comparison Chart)
Table of Contents
Iteration statements allow the set of instructions to execute repeatedly till the condition doesn’t turn out false. The Iteration statements in C++ and Java are, for loop, while loop and do while loop. These statements are commonly called loops.
Here, the main difference between a while loop and do while loop is that while loop check condition before iteration of the loop. On the other hand, the do-while loop verifies the condition after the execution of the statements inside the loop.
Furthermore, the while loop is known as the entry-controlled loop. Conversely, the do while loop is called the exit controlled loop. In this article, we are going to discuss the differences between “while” loop and “do-while” loop.
Content: while Loop Vs do-while Loop
Comparison Chart
Basis for comparison | while | do-while |
---|---|---|
General Form | while ( condition) { statements; //body of loop } | do{ . statements; // body of loop. . } while( Condition ); |
Controlling Condition | In 'while' loop the controlling condition appears at the start of the loop. | In 'do-while' loop the controlling condition appears at the end of the loop. |
Iterations | The iterations do not occur if, the condition at the first iteration, appears false. | The iteration occurs at least once even if the condition is false at the first iteration. |
Alternate name | Entry-controlled loop | Exit-controlled loop |
Semi-colon | Not used | Used at the end of the loop |
Definition of while Loop
The while loop is the most fundamental loop available in C++ and Java. The working of a while loop is similar in both C++ and Java.
Syntax
The general form of while loop is:-
while ( condition) { statements; //body of loop }
The while loop first verifies the condition, and if the condition is true, then, it iterates the loop till the condition turns out false. The condition in while loop can be any boolean expression.
When an expression returns any non-zero value, then the condition is “true”, and if the expression returns a zero value, the condition becomes “false”. If the condition becomes true, then loop iterates itself, and if the condition becomes false, then the control passes to the next line of the code immediately followed by the loop.
The statements or the body of the loop can either be an empty statement or a single statement or a block of statements.
Example of while loop
Let’s discuss the working of a while loop. In the example below the code will print from 1 to 10.
// example is in Java. class example1{ public static void main (String args[]){ int n=0; while(n<10){ n++; System.out.println("n=" +n); } } } //output n=1 n=2 n=3 n=4 n=5 n=6 n=7 n=8 n=9 n=10
Here, the initial value of ‘n’ is 0, which turns the condition in while loop true. The control then enters the body of while loop and the value of ‘n’ is incremented according to the first statement in the body of a while loop. Then the value of ‘n’ is printed and again, the control goes back to the condition of a while loop, now the value of ‘n’ is 1 which again satisfies the condition, and the body of the loop is executed again.
This continues until the condition is true, as soon as the condition becomes false, the loop is terminated. Like for loop, the while loop also first checks the condition and then execute the loop body.
Definition of do-while Loop
As in the while loop, if the controlling condition becomes false in the first iteration only, then the body of the while loop is not executed at all. But the do-while loop is somewhat different from while loop. The do-while loop executes the body of the loop at least once even if the condition is false at the first attempt.
Syntax
The general form of do-while is as follows.
do{ . statements // body of loop. . } while( Condition );
In a do-while loop, the body of loop occurs before the controlling condition, and the conditional statement is at the bottom of the loop. As in while loop, here also, the body of the loop can be empty as both C++ and Java allow null statements or, there can be only a single statement or, a block of statements. The condition here is also a boolean expression, which is true for all non-zero value.
In a do-while loop, the control first reaches to the statement in the body of a do-while loop. The statements in the body get executed first, and then the control reaches the condition part of the loop. The condition is verified and, if it is true, the loop is iterated again, and if the condition is false, then the control resumes to the next line immediately after the loop.
Example of do-while loop
Let’s understand it by implementing the above example in do-while.
// example is in Java. class example2{ public static void main (String args[] ){ int n=1; do{ System.out.println("n=" +n); n++; }while(n<=10); } } //output n=1 n=2 n=3 n=4 n=5 n=6 n=7 n=8 n=9 n=10
Here, the value of n=1 the control resumes to the body of the loop, the value of ‘n’ is printed, and then its value is incremented. Then control resumes to the condition of the do-while loop; the condition is verified, which turns out true for n=1, so, the loop is iterate’s again and continue till the condition become false.
Key Differences Between while and do-while Loop
Conclusion
Both while and do-while loop are the iteration statement, if we want that first, the condition should be verified, and then the statements inside the loop must execute, then the while loop is used. If you want to test the termination condition at the end of the loop, then the do-while loop is used.
ncG1vNJzZmislZi1pbXFn5yrnZ6YsrR6wqikaJyZm7OmvsSnmp5lkprBuLHEp2SwoJmhsm6tzZ1knaddrLWquMRmo6inoGO1tbnL