+ 5
How is a do while loop different from a while loop?
2 Answers
+ 14
a while loop first checks the condition then executes the body, so if the condition is False it never executes.
Do while first executes the body and the checks the condition, so even if the condition is False it will execute once.
+ 3
while is an entry-controlled loop, which means that iteration(s) will take place only if the condition of the loop is true.
e.g.
while(condition) {
:Loop_body
}
do-while on the other hand is a exit-controlled loop, which means that the loop body code will be executed once, after that the condition is evaluated, so even if your condition is wrong, no matter what, your loop will be executed once atleast.
e.g.
do {
:Loop_body
}
while(condition);