Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 577095 of continue

  • 版本网址缩略名: JS-ref/Statements/continue
  • 版本标题: continue
  • 版本 id: 577095
  • 创建于:
  • 创建者: teoli
  • 是否是当前版本?
  • 评论 JavaScript/Reference/Statements/continue JS-ref/Statements/continue

修订内容

概述

结束当前循环语句或标签的执行,然后继续执行循环中的下一次迭代。

版本信息

声明
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

break, label

 

{{ 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" } ) }}

修订版来源

<h3 id=".E6.A6.82.E8.BF.B0">概述</h3>
<p>结束当前循环语句或标签的执行,然后继续执行循环中的下一次迭代。</p>
<h2 id="Version_Information">版本信息</h2>
<table class="standard-table">
  <tbody>
    <tr>
      <td class="header" colspan="2">声明</td>
    </tr>
    <tr>
      <td>Implemented in:</td>
      <td>JavaScript 1.0, NES 2.0</td>
    </tr>
    <tr>
      <td>ECMA Version:</td>
      <td>ECMA-262 (for the unlabeled version)
        <p>ECMA-262, Edition 3 (for the labeled version)</p>
      </td>
    </tr>
  </tbody>
</table>
<h3 id="Syntax" name="Syntax">语法</h3>
<p><code>continue {{ mediawiki.external('<i>label</i>') }}; </code></p>
<h3 id="Parameters" name="Parameters">参数</h3>
<dl>
  <dt>
    <code>label</code></dt>
  <dd>
    Identifier associated with the label of the statement.</dd>
</dl>
<h3 id="Description" name="Description">Description</h3>
<p>In contrast to the <code><a href="/en/JavaScript/Reference/Statements/break" title="en/JavaScript/Reference/Statements/break">break</a></code> statement, <code>continue</code> does not terminate the execution of the loop entirely: instead,</p>
<ul>
  <li>In a <code><a href="/en/JavaScript/Reference/Statements/while" title="en/JavaScript/Reference/Statements/while">while</a></code> loop, it jumps back to the condition.</li>
</ul>
<ul>
  <li>In a <code><a href="/en/JavaScript/Reference/Statements/for" title="en/JavaScript/Reference/Statements/for">for</a></code> loop, it jumps to the update expression.</li>
</ul>
<p>The <code>continue</code> 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 <code>continue</code> statement needs to be nested within this labelled statement.</p>
<h3 id="Examples" name="Examples">Examples</h3>
<h4 id="Example:_Using_continue_with_while" name="Example:_Using_continue_with_while">Example: Using <code>continue</code> with <code>while</code></h4>
<p>The following example shows a <code><a href="/en/JavaScript/Reference/Statements/while" title="en/JavaScript/Reference/Statements/while">while</a></code> loop that has a <code>continue</code> statement that executes when the value of <code>i</code> is 3. Thus, <code>n</code> takes on the values 1, 3, 7, and 12.</p>
<pre class="brush: js">
i = 0;
n = 0;
while (i &lt; 5) {
   i++;
   if (i == 3)
      continue;
   n += i;
}
</pre>
<h4 id="Example:_Using_continue_with_a_label" name="Example:_Using_continue_with_a_label">Example: Using <code>continue</code> with a label</h4>
<p>In the following example, a statement labeled <code>checkiandj</code> contains a statement labeled <code>checkj</code>. If <code>continue</code> is encountered, the program continues at the top of the <code>checkj</code> statement. Each time <code>continue</code> is encountered, <code>checkj</code> reiterates until its condition returns false. When false is returned, the remainder of the <code>checkiandj</code> statement is completed.</p>
<p>If <code>continue</code> had a label of <code>checkiandj</code>, the program would continue at the top of the <code>checkiandj</code> statement.</p>
<pre class="brush: js">
checkiandj:
while (i &lt; 4) {
   document.write(i + "&lt;br&gt;");
   i += 1;

   checkj:
   while (j &gt; 4) {
      document.write(j + "&lt;br&gt;");
      j -= 1;
      if ((j&nbsp;% 2) == 0)
         continue checkj;
      document.write(j + " is odd.&lt;br&gt;");
   }
   document.write("i = " + i + "&lt;br&gt;");
   document.write("j = " + j + "&lt;br&gt;");
}
</pre>
<h3 id="See_also" name="See_also">See also</h3>
<p><a href="/en/JavaScript/Reference/Statements/break" title="en/JavaScript/Reference/Statements/break">break</a>, <a href="/en/JavaScript/Reference/Statements/label" title="en/JavaScript/Reference/Statements/label">label</a></p>
<p>&nbsp;</p>
<p>{{ 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" } ) }}</p>
恢复到这个版本