JavaScript – Continue Statement


JavaScript – Continue Statement


”;


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.

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

The syntax of continue statement in JavaScript is as follows −

continue;
OR
continue label;

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

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

Continue statement with for loop

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.

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>

Output

Entering the loop.
1
2
4
Exiting the loop!

Continue statement with while loop

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.

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>

Output

Entering the loop.
4
5
Exiting the loop!

Continue statement with the nested loop

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

Example

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.

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>

Output

Entering the loop.
1 - 1
1 - 2
1 - 4
4 - 1
4 - 2
4 - 4
Exiting the loop!

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *