In do-while loop, first attempt of loop should be executed then it check the condition.
The benefit of do-while loop/statement is that we get entry in loop and then condition will check for very first time.
In while loop, condition will check first and if condition will not satisfied then the loop will not execute any further.
Value of j variable is: 0 Value of j variable is: 1 Value of j variable is: 2 Value of j variable is: 3
do-while | while |
---|---|
Condition is checked at the end of the loop. | Condition is checked in the beginning of the loop. |
Set of statements within the loop will be executed atleast once. | If the condition is fals, the statements will not be executed even once. |