I have a short javascript code where I need to skip to next in the for loop....see below:
var y = new Array ('1', '2', '3', '4'); for (var x in y) { callFunctionOne(y[x]); while (condition){ condition = callFunctionTwo(y[x]); //now want to move to the next item so // invoke callFunctionTwo() again... } }
Wanted to keep it simple so syntax may be error free.
The continue statement in Python is used to skip the rest of the code inside a loop for the current iteration only. In other words, the loop will not terminate immediately but it will continue on with the next iteration. This is in contrast with the break statement which will terminate the loop completely.
You can use the continue statement if you need to skip the current iteration of a for or while loop and move onto the next iteration.
The continue statement (with or without a label reference) can only be used to skip one loop iteration. The break statement, without a label reference, can only be used to jump out of a loop or a switch.
The continue statement terminates execution of the statements in the current iteration of the current or labeled loop, and continues execution of the loop with the next iteration.
Don't iterate over arrays using for...in
. That syntax is for iterating over the properties of an object, which isn't what you're after.
As for your actual question, you can use the continue
:
var y = [1, 2, 3, 4]; for (var i = 0; i < y.length; i++) { if (y[i] == 2) { continue; } console.log(y[i]); }
This will print:
1 3 4
Actually, it looks like you want to break out of the while
loop. You can use break
for that:
while (condition){ condition = callFunctionTwo(y[x]); break; }
Take a look at do...while
loops as well.
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