Using continue in a do-while loop
Asked Answered
P

7

19

MDN states:

When you use continue without a label, it terminates the current iteration of the innermost enclosing while, do-while or for statement and continues execution of the loop with the next iteration.

I'm not sure why the following piece of code does not work as I expect.

do {
  continue;
} while(false);

Even though the while condition is false, I expect it to run forever since continue jumps towards the start of the block, which immediately executes continue again, etc. Somehow however, the loop terminates after one iteration. It looks like continue is ignored.

How does continue in a do-while loop work?

Pop answered 30/6, 2012 at 13:20 Comment(3)
The condition being met is implied. If the condition isn't met, there's no next iteration.Felicific
I missed that. I guess I was misled by the order of the source code. (I thought continue would always "jump up" in the source code.)Pop
It does seem that way, especially if you use a statement label. TOP: do { continue TOP; } while(false)Felicific
L
14

Check out this jsFiddle: http://jsfiddle.net/YdpJ2/3/

var getFalse = function() {
  alert("Called getFalse!");
  return false;
};

do {
  continue;
  alert("Past the continue? That's impossible.");
} while( getFalse() );​

It appears to hit the continue, then break out of that iteration to run the check condition. Since the condition is false, it terminates.

Lawtun answered 30/6, 2012 at 13:27 Comment(0)
B
12

continue does not skip the check while(false) but simply ignores the rest of the code within the brackets.

Benisch answered 30/6, 2012 at 13:23 Comment(0)
A
5

Continue stops execution of the rest of the code in the block and jumps directly to the next iteration of your loop.

Since you are doing while(false) there is no next iteration

Aga answered 30/6, 2012 at 13:23 Comment(0)
S
5

continue doesn't start over the current iteration again but skips to the next one (as said in the MDN-quote).

because of a false condition, there is no next iteration - so the whole loop is completed.

Sexpartite answered 30/6, 2012 at 13:23 Comment(0)
W
4

I expect it to run forever since continue jumps towards the start of the block

The continue doesn't jump to the start of the block, it jumps to the end of the block.

Werby answered 30/6, 2012 at 13:40 Comment(0)
T
3

After the continue, the loop conditional is evaluated, since it is false, the loop will terminate.

Trek answered 30/6, 2012 at 13:22 Comment(0)
C
1

Use while(true) to get an infinite loop.

If you replaced while(false) with while(true), you would get an infinite loop.

do {
  continue;
} while(true);

continue skips to the end of the block, which in a do-while loop, will execute the conditional statement.

In your case, your condition was false and therefore it terminates the loop. If that condition is true, it would continue at the start of the loop.

Closing answered 8/6, 2020 at 18:56 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.