1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
---
title: continue
slug: Web/JavaScript/Reference/Statements/continue
translation_of: Web/JavaScript/Reference/Statements/continue
---
<div>
<div>{{jsSidebar("Statements")}}</div>
</div>
<p><strong>continue 声明</strong>终止当前循环或标记循环的当前迭代中的语句执行,并在下一次迭代时继续执行循环。</p>
<p>{{EmbedInteractiveExample("pages/js/statement-continue.html")}}</p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox">continue [ label ];</pre>
<dl>
</dl>
<dl>
<dt><code>label</code></dt>
<dd>标识标号关联的语句</dd>
</dl>
<h2 id="Description" name="Description">描述</h2>
<p>与 {{jsxref("Statements/break", "break")}} 语句的区别在于, continue 并不会终止循环的迭代,而是:</p>
<ul>
<li>在 {{jsxref("Statements/while", "while")}} 循环中,控制流跳转回条件判断;</li>
</ul>
<ul>
<li>在 {{jsxref("Statements/for", "for")}} 循环中,控制流跳转到更新语句。</li>
</ul>
<p><code>continue</code> 语句可以包含一个可选的标号以控制程序跳转到指定循环的下一次迭代,而非当前循环。此时要求 <code>continue</code> 语句在对应的循环内部。</p>
<h2 id="Examples" name="Examples">示例</h2>
<h3 id="Example_Using_continue_with_while" name="Example:_Using_continue_with_while">在 <code>while</code> 语句中使用 <code>continue</code></h3>
<p>下述例子展示了一个在<code>i</code> 为 3时执行<code>continue</code> 语句的 {{jsxref("Statements/while", "while")}} 循环。因此,<code>n</code> 的值在几次迭代后分别为 1, 3, 7 和 12 .</p>
<pre class="brush: js language-js"><code class="language-js">i <span class="operator token">=</span> <span class="number token">0</span><span class="punctuation token">;</span>
n <span class="operator token">=</span> <span class="number token">0</span><span class="punctuation token">;</span>
<span class="keyword token">while</span> <span class="punctuation token">(</span>i <span class="operator token"><</span> <span class="number token">5</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
i<span class="operator token">++</span><span class="punctuation token">;</span>
<span class="keyword token">if</span> <span class="punctuation token">(</span>i <span class="operator token">===</span> <span class="number token">3</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
<span class="keyword token">continue</span><span class="punctuation token">;</span>
<span class="punctuation token">}</span>
n <span class="operator token">+</span><span class="operator token">=</span> i<span class="punctuation token">;</span>
<span class="punctuation token">}</span></code></pre>
<h3 id="使用带_label_的_continue">使用带 label 的 continue</h3>
<p>在下面的例子中,被标记为 <code>checkiandj</code> 的语句包含一个被标记为 <code>checkj</code> 的语句。当遇到<code>continue</code> 语句时,程序回到 <code>checkj</code> 语句的开始继续执行。每次遇到 <code>continue</code> 时,再次执行 <code>checkj</code> ,直到条件判断返回 false 。之后完成 <code>checkiandj</code> 语句剩下的部分。</p>
<p>但如果 <code>continue</code> 的标号被改为 <code>checkiandj</code> ,那程序将会从 <code>checkiandj</code> 语句的开始继续运行。</p>
<p>参考 {{jsxref("Statements/label", "label")}} 。</p>
<pre class="brush: js language-js"><code class="language-js"><span class="keyword token">var</span> i <span class="operator token">=</span> <span class="number token">0</span><span class="punctuation token">,</span>
j <span class="operator token">=</span> <span class="number token">8</span><span class="punctuation token">;</span>
checkiandj<span class="punctuation token">:</span> <span class="keyword token">while</span> <span class="punctuation token">(</span>i <span class="operator token"><</span> <span class="number token">4</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
console<span class="punctuation token">.</span><span class="function token">log<span class="punctuation token">(</span></span><span class="string token">"i: "</span> <span class="operator token">+</span> i<span class="punctuation token">)</span><span class="punctuation token">;</span>
i <span class="operator token">+</span><span class="operator token">=</span> <span class="number token">1</span><span class="punctuation token">;</span>
checkj<span class="punctuation token">:</span> <span class="keyword token">while</span> <span class="punctuation token">(</span>j <span class="operator token">></span> <span class="number token">4</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
console<span class="punctuation token">.</span><span class="function token">log<span class="punctuation token">(</span></span><span class="string token">"j: "</span><span class="operator token">+</span> j<span class="punctuation token">)</span><span class="punctuation token">;</span>
j <span class="operator token">-</span><span class="operator token">=</span> <span class="number token">1</span><span class="punctuation token">;</span>
<span class="keyword token">if</span> <span class="punctuation token">(</span><span class="punctuation token">(</span>j <span class="operator token">%</span> <span class="number token">2</span><span class="punctuation token">)</span> <span class="operator token">==</span> <span class="number token">0</span><span class="punctuation token">)</span>
<span class="keyword token">continue</span> checkj<span class="punctuation token">;</span>
console<span class="punctuation token">.</span><span class="function token">log<span class="punctuation token">(</span></span>j <span class="operator token">+</span> <span class="string token">" is odd."</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="punctuation token">}</span>
console<span class="punctuation token">.</span><span class="function token">log<span class="punctuation token">(</span></span><span class="string token">"i = "</span> <span class="operator token">+</span> i<span class="punctuation token">)</span><span class="punctuation token">;</span>
console<span class="punctuation token">.</span><span class="function token">log<span class="punctuation token">(</span></span><span class="string token">"j = "</span> <span class="operator token">+</span> j<span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="punctuation token">}</span></code></pre>
<p>输出:</p>
<pre class="brush: js language-js"><code class="language-js"><span class="string token">"i: 0"</span>
<span class="comment token">
// start checkj
</span><span class="string token">"j: 8"</span>
<span class="string token">"7 is odd."</span>
<span class="string token">"j: 7"</span>
<span class="string token">"j: 6"</span>
<span class="string token">"5 is odd."</span>
<span class="string token">"j: 5"</span><span class="comment token">
// end checkj
</span>
<span class="string token">"i = 1"</span>
<span class="string token">"j = 4"</span>
<span class="string token">"i: 1"</span>
<span class="string token">"i = 2"</span>
<span class="string token">"j = 4"</span>
<span class="string token">"i: 2"</span>
<span class="string token">"i = 3"</span>
<span class="string token">"j = 4"</span>
<span class="string token">"i: 3"</span>
<span class="string token">"i = 4"</span>
<span class="string token">"j = 4"</span></code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('ES1')}}</td>
<td>{{Spec2('ES1')}}</td>
<td>Initial definition. Unlabeled version.</td>
</tr>
<tr>
<td>{{SpecName('ES3')}}</td>
<td>{{Spec2('ES3')}}</td>
<td>Labeled version added.</td>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-12.7', 'Continue statement')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-continue-statement', 'Continue statement')}}</td>
<td>{{Spec2('ES6')}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-continue-statement', 'Continue statement')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>{{Compat("javascript.statements.continue")}}</p>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
<li>{{jsxref("Statements/break", "break")}}</li>
<li>{{jsxref("Statements/label", "label")}}</li>
</ul>
|