Core JavaScript 1.5 Guide:Loop Statements:continue Statement
From MDC
[edit] continue Statement
The continue statement can be used to restart a while, do-while, for, or label statement.
- When you use
continuewithout a label, it terminates the current iteration of the innermost enclosingwhile, do-whileorforstatement and continues execution of the loop with the next iteration. In contrast to thebreakstatement,continuedoes not terminate the execution of the loop entirely. In awhileloop, it jumps back to the condition. In aforloop, it jumps to the increment-expression. - When you use
continuewith alabel, it applies to the looping statement identified with thatlabel.
The syntax of the continue statement looks like the following:
-
continue -
continue label
Example 1
The following example shows a while loop with a continue statement that executes when the value of i is three. Thus, n takes on the values one, three, seven, and twelve.
i = 0;
n = 0;
while (i < 5) {
i++;
if (i == 3)
continue;
n += i;
}
Example 2
A statement labeled checkiandj contains a statement labeled checkj. If continue is encountered, the program terminates the current iteration of checkj and begins the next iteration. Each time continue is encountered, checkj reiterates until its condition returns false. When false is returned, the remainder of the checkiandj statement is completed, and checkiandj reiterates until its condition returns false. When false is returned, the program continues at the statement following checkiandj.
If continue had a label of checkiandj, the program would continue at the top of the checkiandj statement.
checkiandj :
while (i < 4) {
document.write(i + "<br/>");
i += 1;
checkj :
while (j > 4) {
document.write(j + "<br/>");
j -= 1;
if ((j % 2) == 0)
continue checkj;
document.write(j + " is odd.<br/>");
}
document.write("i = " + i + "<br/>");
document.write("j = " + j + "<br/>");
}