continue

Did you know that you can read content offline by using one of these tools? If you would like to read offline MDN content in another format, let us know by commenting on Bug 665750.

Dash App
  • Tags
  • Files

Summary

Terminates execution of the statements in the current iteration of the current or labelled loop, and continues execution of the loop with the next iteration.

Version Information

Statement
Implemented in: JavaScript 1.0, NES 2.0
ECMA Version: ECMA-262 (for the unlabeled version)

ECMA-262, Edition 3 (for the labeled version)

Syntax

continue [label];

Parameters

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