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 648837 of continue

  • Revision slug: Web/JavaScript/Reference/Statements/continue
  • Revision title: continue
  • Revision id: 648837
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment

Revision Content

{{JsStatementsQLAlpha()}}

Summary

The continue statement terminates execution of the statements in the current iteration of the current or labeled loop, and continues execution of the loop with the next iteration.

Syntax

continue [ label ];

Parameters

label
Identifier associated with the label of the statement.

Description

In contrast to the {{jsxref("Statements/break", "break")}} statement, continue does not terminate the execution of the loop entirely: instead,

  • In a {{jsxref("Statements/while", "while")}} loop, it jumps back to the condition.
  • In a {{jsxref("Statements/for", "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 labeled loop statement instead of the current loop. In this case, the continue statement needs to be nested within this labeled statement.

Examples

Example: Using continue with while

The following example shows a {{jsxref("Statements/while", "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;
}

<h3example: using="" code="">Example: Continue with a label</h3example:>

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.

var i = 0,
    j = 0;

checkiandj: while (i < 4) {
   console.log(i);
   i += 1;

   checkj: while (j > 4) {
      console.log(j);
      j -= 1;
      if ((j % 2) == 0)
         continue checkj;
      console.log(j + " is odd.");
   }
   console.log("i = " + i);
   console.log("j = " + j);
}

Output:

0
"i = 1"
"j = 0"

1
"i = 2"
"j = 0" 

2 
"i = 3"
"j = 0"

3
"i = 4"
"j = 0"

Specifications

Specification Status Comment
ECMAScript 1st Edition Standard Initial definition. Unlabeled version.
ECMAScript 3rd Edition Standard Labeled version added.
{{SpecName('ES5.1', '#sec-12.7', 'Continue statement')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-continue-statement', 'Continue statement')}} {{Spec2('ES6')}}  

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}

See also

  • {{jsxref("Statements/break", "break")}}
  • {{jsxref("Statements/label", "label")}}

Revision Source

<div>
 {{JsStatementsQLAlpha()}}</div>
<h2 id="Summary">Summary</h2>
<p>The <strong>continue statement</strong> terminates execution of the statements in the current iteration of the current or labeled loop, and continues execution of the loop with the next iteration.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
continue [ label ];</pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
 <dt>
  <code>label</code></dt>
 <dd>
  Identifier associated with the label of the statement.</dd>
</dl>
<h2 id="Description" name="Description">Description</h2>
<p>In contrast to the {{jsxref("Statements/break", "break")}} statement, <code>continue</code> does not terminate the execution of the loop entirely: instead,</p>
<ul>
 <li>In a {{jsxref("Statements/while", "while")}} loop, it jumps back to the condition.</li>
</ul>
<ul>
 <li>In a {{jsxref("Statements/for", "for")}} 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 labeled loop statement instead of the current loop. In this case, the <code>continue</code> statement needs to be nested within this labeled statement.</p>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Using_continue_with_while" name="Example:_Using_continue_with_while">Example: Using <code>continue</code> with <code>while</code></h3>
<p>The following example shows a {{jsxref("Statements/while", "while")}} 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>
<h3><h3example: code="" using="">Example: <code>Continue</code> with a label</h3example:></h3>
<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">
var i = 0,
&nbsp;&nbsp;&nbsp; j = 0;

checkiandj: while (i &lt; 4) {
&nbsp;&nbsp; console.log(i);
&nbsp;&nbsp; i += 1;

&nbsp;&nbsp; checkj: while (j &gt; 4) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; console.log(j);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; j -= 1;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ((j % 2) == 0)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; continue checkj;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; console.log(j + " is odd.");
&nbsp;&nbsp; }
&nbsp;&nbsp; console.log("i = " + i);
&nbsp;&nbsp; console.log("j = " + j);
}
</pre>
<p>Output:</p>
<pre class="brush: js">
0
"i = 1"
"j = 0"

1
"i = 2"
"j = 0" 

2 
"i = 3"
"j = 0"

3
"i = 4"
"j = 0"
</pre>
<h2 id="Specifications">Specifications</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>{{SpecName('ES5.1', '#sec-12.7', 'Continue statement')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-continue-statement', 'Continue statement')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</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>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</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>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
 <li>{{jsxref("Statements/break", "break")}}</li>
 <li>{{jsxref("Statements/label", "label")}}</li>
</ul>
Revert to this revision