2019-04-21 11:50:48 +08:00

176 lines
12 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article id="wikiArticle">
<div>
<div></div>
</div>
<h2 id="概述">概述</h2>
<p><strong>continue 语句</strong>结束当前(或标签)的循环语句的本次迭代,并继续执行循环的下一次迭代。</p>
<h2 id="语法">语法</h2>
<pre><code class="language-javascript">continue [ label ];</code></pre>
<dl>
</dl>
<dl>
<dt><code>label</code></dt>
<dd>标识标号关联的语句</dd>
</dl>
<h2 id="Description" name="Description">描述</h2>
<p><a href="Reference/Statements/break" title="break 语句中止当前循环switch语句或label 语句,并把程序控制流转到紧接着被中止语句后面的语句。"><code>break</code></a> 语句的区别在于, continue 并不会终止循环的迭代,而是:</p>
<ul>
<li><a href="Reference/Statements/while" title="while 语句可以在某个条件表达式为真的前提下,循环执行指定的一段代码,直到那个表达式不为真时结束循环。"><code>while</code></a> 循环中,控制流跳转回条件判断;</li>
</ul>
<ul>
<li><a href="Reference/Statements/for" title="for 语句用于创建一个循环,它包含了三个可选的表达式,三个可选的表达式包围在圆括号中并由分号分隔, 后跟一个在循环中执行的语句(通常是一个块语句)。"><code>for</code></a> 循环中,控制流跳转到更新语句。</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>下述例子展示了 <a href="Reference/Statements/while" title="while 语句可以在某个条件表达式为真的前提下,循环执行指定的一段代码,直到那个表达式不为真时结束循环。"><code>while</code></a> 循环中 <code>continue</code> 语句的使用。当循环到 <code>i</code> 的值为 3 时,执行 continue 。 n 的值在几次迭代后分别为 1, 3, 7 和 12 </p>
<pre><code class="language-js language-js"><code class="language-js">i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
n <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">while</span> <span class="token punctuation">(</span>i <span class="token operator">&lt;</span> <span class="token number">5</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
i<span class="token operator">++</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">===</span> <span class="token number">3</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">continue</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
n <span class="token operator">+</span><span class="token operator">=</span> i<span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></code></pre>
<h3 id="例子使用带标号的_continue">例子:使用带标号的 <code>continue</code></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>参考 <a href="Reference/Statements/label" title="标记语句可以和 break 或 continue 语句一起使用。标记就是在一条语句前面加个可以引用的标识符。"><code>label</code></a></p>
<pre><code class="language-js language-js"><code class="language-js"><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span>
j <span class="token operator">=</span> <span class="token number">8</span><span class="token punctuation">;</span>
checkiandj<span class="token punctuation">:</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>i <span class="token operator">&lt;</span> <span class="token number">4</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span><span class="token string">"i: "</span> <span class="token operator">+</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span>
i <span class="token operator">+</span><span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
checkj<span class="token punctuation">:</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>j <span class="token operator">&gt;</span> <span class="token number">4</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span><span class="token string">"j: "</span><span class="token operator">+</span> j<span class="token punctuation">)</span><span class="token punctuation">;</span>
j <span class="token operator">-</span><span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>j <span class="token operator">%</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span>
<span class="token keyword">continue</span> checkj<span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span>j <span class="token operator">+</span> <span class="token string">" is odd."</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span><span class="token string">"i = "</span> <span class="token operator">+</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span><span class="token string">"j = "</span> <span class="token operator">+</span> j<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></code></pre>
<p>输出:</p>
<pre><code class="language-js language-js"><code class="language-js"><span class="token string">"i: 0"</span>
<span class="token comment">
// start checkj
</span><span class="token string">"j: 8"</span>
<span class="token string">"7 is odd."</span>
<span class="token string">"j: 7"</span>
<span class="token string">"j: 6"</span>
<span class="token string">"5 is odd."</span>
<span class="token string">"j: 5"</span><span class="token comment">
// end checkj
</span>
<span class="token string">"i = 1"</span>
<span class="token string">"j = 4"</span>
<span class="token string">"i: 1"</span>
<span class="token string">"i = 2"</span>
<span class="token string">"j = 4"</span>
<span class="token string">"i: 2"</span>
<span class="token string">"i = 3"</span>
<span class="token string">"j = 4"</span>
<span class="token string">"i: 3"</span>
<span class="token string">"i = 4"</span>
<span class="token string">"j = 4"</span></code></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>ECMAScript 1st Edition</td>
<td>Standard</td>
<td>Initial definition. Unlabeled version.</td>
</tr>
<tr>
<td>ECMAScript 3rd Edition</td>
<td>Standard</td>
<td>Labeled version added.</td>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/5.1/#sec-12.7" hreflang="en" lang="en" rel="noopener">ECMAScript 5.1 (ECMA-262)<br/><small lang="zh-CN">Continue statement</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td> </td>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-continue-statement" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Continue statement</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p></p><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
<strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
<div class="htab">
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
</div><p></p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
</tr>
</tbody>
</table>
</div>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
<li><a href="Reference/Statements/break" title="break 语句中止当前循环switch语句或label 语句,并把程序控制流转到紧接着被中止语句后面的语句。"><code>break</code></a></li>
<li><a href="Reference/Statements/label" title="标记语句可以和 break 或 continue 语句一起使用。标记就是在一条语句前面加个可以引用的标识符。"><code>label</code></a></li>
</ul>
</article>