Core JavaScript 1.5 Reference:Statements:continue
From MDC
Contents |
[edit] Summary
Terminates execution of the statements in the current iteration of the current or labelled loop, and continues execution of the loop with the next iteration.
| Statement | |
| Implemented in: | JavaScript 1.0, NES 2.0 |
| ECMA Version: | ECMA-262 (for the unlabeled version)
ECMA-262, Edition 3 (for the labeled version) |
[edit] Syntax
continue [label];
[edit] Parameters
-
label - Identifier associated with the label of the statement.
[edit] Description
In contrast to the break statement, continue does not terminate the execution of the loop entirely: instead,
- In a
whileloop, it jumps back to the condition.
- In a
forloop, it jumps to the update expression.
The continue statement can include an optional label that allows the program to jump to the next iteration of a labelled loop statement instead of the current loop. In this case, the continue statement needs to be nested within this labelled statement.
[edit] Examples
[edit] Example: Using continue with while
The following example shows a while loop that has a continue statement that executes when the value of i is 3. Thus, n takes on the values 1, 3, 7, and 12.
i = 0;
n = 0;
while (i < 5) {
i++;
if (i == 3)
continue;
n += i;
}
[edit] Example: Using continue with a label
In the following example, a statement labeled checkiandj contains a statement labeled checkj. If continue is encountered, the program continues at the top of the checkj statement. Each time continue is encountered, checkj reiterates until its condition returns false. When false is returned, the remainder of the checkiandj statement is completed.
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>");
}