Javascript 简明教程

JavaScript - Continue Statement

JavaScript 中的 continue 语句用于跳过循环的当前迭代并继续执行下一个迭代。它通常与 if 语句结合使用,以检查条件,并在满足条件时跳过迭代。

The continue statement in JavaScript is used to skip the current iteration of a loop and continue with the next iteration. It is often used in conjunction with an if statement to check for a condition and skip the iteration if the condition is met.

JavaScript continue 语句告诉解释器立即开始循环的下一个迭代并跳过剩余的代码块。遇到 continue 语句时,程序流立即移动到循环检查表达式,并且如果条件仍然为真,则开始下一个迭代,否则跳出循环。

The JavaScript continue statement tells the interpreter to immediately start the next iteration of the loop and skip the remaining code block. When a continue statement is encountered, the program flow moves to the loop check expression immediately and if the condition remains true, then it starts the next iteration, otherwise the control comes out of the loop.

Syntax

JavaScript 中 continue 语句的语法如下:

The syntax of continue statement in JavaScript is as follows −

continue;
OR
continue label;

我们可以在诸如 for 循环、while 循环、do…while 循环等循环中使用 continue 语句。

We can use the continue statement inside the loops like for loop, while loop, do…while loop, etc.

我们将在下一章学习在 'continue' 语句后使用 'label' 语句。

We will learn to use the ‘continue’ statement with the ‘label’ statement in the upcoming chapter.

Continue statement with for loop

以下示例将 for 循环与 continue 语句一起使用。在循环中,当 x 的值是 3 时,它将执行 continue 语句以跳过当前迭代并移动到下一个迭代。

The example below uses the continue statement with the for loop. In the loop, when the value of the x is 3, it will execute the continue statement to skip the current iteration and move to the next iteration.

在输出中,你可以看到循环没有打印 3。

In the output, you can see that the loop doesn’t print 3.

Example

<html>
<head>
   <title> JavaScript - Continue statement </title>
</head>
<body>
   <p id = "output"> </p>
   <script>
      let output = document.getElementById("output");
      output.innerHTML += "Entering the loop. <br /> ";
      for (let x = 1; x < 5; x++) {
         if (x == 3) {
            continue;   // skip rest of the loop body
         }
         output.innerHTML += x + "<br />";
      }
      output.innerHTML += "Exiting the loop!<br /> ";
   </script>
</body>
</html>
Entering the loop.
1
2
4
Exiting the loop!

Continue statement with while loop

我们在以下示例中将 while 循环与 continue 语句一起使用。在 while 循环的每次迭代中,我们将 x 的值增加 1。如果 x 的值为 2 或 3,则它将跳过当前迭代并移动到下一个迭代。

We used the while loop with the continue statement in the example below. In each iteration of the while loop, we increment the x’s value by 1. If the value of the x is equal to 2 or 3, it skips the current iteration and moves to the next iteration.

在输出中,你可以看到代码不会打印 2 或 3。

In the output, you can see that the code doesn’t print 2 or 3.

Example

<html>
<head>
   <title> JavaScript - Continue statement </title>
</head>
<body>
   <p id = "output"> </p>
   <script>
      let output = document.getElementById("output");
      var x = 1;
      output.innerHTML += "Entering the loop. <br /> ";
      while (x < 5) {
         x = x + 1;
         if (x == 2 || x == 3) {
            continue;   // skip rest of the loop body
         }
         output.innerHTML += x + "<br />";
      }
      output.innerHTML += "Exiting the loop!<br /> ";
   </script>
</body>
</html>
Entering the loop.
4
5
Exiting the loop!

Continue statement with the nested loop

你可以将 continue 语句与嵌套循环一起使用并跳过父循环或子循环的迭代。

You can use the continue statement with nested loops and skip the iteration of the parent loop or child loop.

Example

在下面的代码中,父循环遍历 1 到 5 个元素。在父循环中,我们使用 continue 语句来跳过 x 的值为 2 或 3 时的迭代。此外,我们还定义了嵌套循环。在嵌套循环中,当 y 的值为 3 时跳过循环迭代。

The parent loop traverses the 1 to 5 elements in the code below. In the parent loop, we used the continue statement to skip the iteration when the value of x is 2 or 3. Also, we have defined the nested loop. In the nested loop, we skip the loop iteration when the value of y is 3.

在输出中,你可以观察 x 和 y 的值。你不会看到 x 的 2 或 3 值以及 y 的 3 值。

In the output, you can observe the value of x and y. You won’t see 2 or 3 values for x and 3 for y.

<html>
<head>
   <title> JavaScript - Continue statement </title>
</head>
<body>
   <p id = "output"> </p>
   <script>
      let output = document.getElementById("output");
      output.innerHTML += "Entering the loop. <br /> ";
      for (let x = 1; x < 5; x++) {
         if (x == 2 || x == 3) {
            continue;   // skip rest of the loop body
         }
         for (let y = 1; y < 5; y++) {
            if (y == 3) {
               continue;
            }
            output.innerHTML += x + " - " + y + "<br />";
         }
      }
      output.innerHTML += "Exiting the loop!<br /> ";
   </script>
</body>
</html>
Entering the loop.
1 - 1
1 - 2
1 - 4
4 - 1
4 - 2
4 - 4
Exiting the loop!