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
- The while loop checks the condition at the starting of the loop and if the condition is satisfied statement inside the loop, is executed. As against, in the do-while loop, the condition is checked after the execution of all statements in the body of the loop.
- If the condition in a while loop is false, not a single statement inside the loop is executed. In contrast, if the condition in ‘do-while’ loop is false, then also the body of the loop is executed at least once then the condition is tested.
- The while loop is also known as the entry-controlled and pre-checking loop because in this loop, the body of the loop is executed prior to checking the condition. Conversely, the alternate name for the do-while loop is the exit-controlled and post-checking loop, the reason behind this is that the checking of the loop condition is followed by the execution of the body of the loop.
- The syntax of a do-while loop includes a semi-colon to terminate the loop. On the contrary, there is no use of the semi-colon in the while loop syntax.
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.
Shripad Tambat says
I loved this page very nicely presented and easy answers with key points covered in it……
NIT says
Very helpful for all users
Rajnish kumar says
Well explained!
Anandhu says
Nice notes
Jan van der Zanden says
Pascal is more clean and clear:
Repeat
….
statements;
..
Until (condition);
In stead of
Do
{
….
}
While (condition);
Elansuriyan says
Super Sis
lala says
Well explained!!!!
ksr says
Thanks..well explained
Sujal says
Nice I like it thanks 😘💞😊
Justin Dodd says
This is how the understanding of loops should be taught!!!!!! great job:)
Rebecca Kavinga says
Nice notes they have really helped me, thanks
Pauline says
Well explained