概述
结束当前循环语句或标签的执行,然后继续执行循环中的下一次迭代。
版本信息
声明 | |
Implemented in: | JavaScript 1.0, NES 2.0 |
ECMA Version: | ECMA-262 (for the unlabeled version)
ECMA-262, Edition 3 (for the labeled version) |
语法
continue {{ mediawiki.external('label') }};
参数
-
label
- Identifier associated with the label of the statement.
Description
In contrast to the break
statement, continue
does not terminate the execution of the loop entirely: instead,
- In a
while
loop, it jumps back to the condition.
- In a
for
loop, 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.
Examples
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; }
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>"); }
See also
{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Sentencias/continue", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Instructions/continue", "ja": "ja/Core_JavaScript_1.5_Reference/Statements/continue", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Polecenia/continue" } ) }}