10

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.

Kaspar Lee
  • 5,446
  • 4
  • 31
  • 54
Shaz
  • 15,637
  • 3
  • 41
  • 59
  • @lwburk: I'm not asking about other loops (`for`, `switch`, `if`). I'm wondering what the difference between `while` and `do-while` is. – Shaz Apr 08 '11 at 18:09
  • Non sequitur. That's exactly what that question was about. – Wayne Apr 08 '11 at 18:14
  • Maybe you'd prefer: http://stackoverflow.com/questions/3347001/do-while-vs-while or http://stackoverflow.com/questions/390605/while-vs-do-while or http://stackoverflow.com/questions/3094972/when-should-i-use-do-while-instead-of-while-loops or http://stackoverflow.com/questions/224059/test-loops-at-the-top-or-bottom-while-vs-do-while or http://stackoverflow.com/questions/994905/is-there-ever-a-need-for-a-do-while-loop – Wayne Apr 08 '11 at 18:20
  • This question could still be usefully considered a non-duplicate because it is specifically scoped to JavaScript. There may be certain gotchas or discouragements for its usage not relevant to other languages. However, at least at this time, there appear to be none of those. – derpedy-doo Sep 11 '19 at 20:52
  • Whoever decided that this question is a duplicate may not have read the whole question. The other question just asks when you would use `do … while`, not how it differs from `while …`. – Manngo Jun 30 '23 at 04:52

6 Answers6

29

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.

Tejs
  • 40,736
  • 10
  • 68
  • 86
11

The do-while is guaranteed to run at least once. While the while loop may not run at all.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
2

do while checks the conditional after the block is run. while checks the conditional before it is run. This is usually used in situations where the code is always run at least once.

voidzm
  • 649
  • 1
  • 4
  • 8
1

The do statement normally ensures your code gets executed at least once (expression evaluated at the end), whilst while evaluates at the start.

Rich
  • 3,640
  • 3
  • 20
  • 24
1

Lets say you wanted to process the block inside the loop at least once, regardless of the condition.

Noah Campbell
  • 1,042
  • 9
  • 13
1

if you would get the counterTwo value as a return value of another function, you would safe in the first case an if statement.

e.g.

var counterTwo = something.length; 

while(counterTwo > 0) {
    counterTwo--;
    document.write(something.get(counterTwo));
}

or

var counterTwo = something.length; 

if(counterTwo < 0) return;

do
{
        counterTwo--;
    document.write(something.get(counterTwo));
} while(counterTwo > 0);

the first case is useful, if you process the data in an existing array. the second case is useful, if you "gather" the data:

do
{
     a = getdata();
     list.push(a);
} while(a != "i'm the last item");
user287107
  • 9,286
  • 1
  • 31
  • 47