MDN states:
When you use
continue
without a label, it terminates the current iteration of the innermost enclosingwhile
,do-while
orfor
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?
Short answer yes, continue (and break) work properly in do while loops.
For the for loop, continue statement causes the conditional test and increment portions of the loop to execute. For the while and do... while loops, continue statement causes the program control to pass to the conditional tests.
The continue statement gives you the option to skip over the part of a loop where an external condition is triggered, but to go on to complete the rest of the loop. That is, the current iteration of the loop will be disrupted, but the program will return to the top of the loop.
Continue statement in java can be used with for , while and do-while loop.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With