Possible Duplicate:
When is a do-while appropriate?
Would someone mind telling me what the difference between these two statements are and when one should be used over the other?
var counterOne = -1;
do {
counterOne++;
document.write(counterOne);
} while(counterOne < 10);
Or:
var counterTwo = -1;
while(counterTwo < 10) {
counterTwo++;
document.write(counterTwo);
}
http://fiddle.jshell.net/Shaz/g6JS4/
At this moment in time I don't see the point of the do
statement if it can just be done without specifying it inside the while
statement.
Do / While VS While is a matter of when the condition is checked.
A while loop checks the condition, then executes the loop. A Do/While executes the loop and then checks the conditions.
For example, if the counterTwo
variable was 10 or greater, then do/while loop would execute once, while your normal while loop would not execute the loop.