+ 2
What is difference b/w do while and while loop
4 Answers
+ 5
do-while loop will execute a portion/block of your code before checking a condition.
on the other hand a while loop will check before executing a code block.
Is kinda like saying u want to go to ur friend's house to play video game. You could go to his home to check on him and find out he's not around, that's do-while
Or you could call him on phone to find out if he's home before going; this is while.
+ 2
do while always executes at least once. while(x) is the equivalent of if(x) {do... while(x)}
+ 1
A do..while loop executes the instructions in its loop body before checking the loop condition. Contrary, a while loop checks the loop condition prior to executing instructions in its loop body, so you need to assess which of those loops best fits the requirement.
Syntactically;
do
{
// loop body
} while(condition);
Loop body is executed once, then condition is evaluated, the loop continues if condition evaluates to true.
Therefore in do...while loop the loop body will be executed atleast once!
Note, mind the semicolon after (condition).
==============================
while(condition)
{
// loop body
}
Condition is checked first, loop body is executed only IF condition evaluates to true.
- by Ipang
+ 1
When we use while loop, it FIRST CHECKS whether the condition is true or false and then apply the loop. But if we use do_while loop, it will perform the loop at least one time, and then will check if the condition is true or false.
-by veeresh maurya