JavaScript Continue statement; In this tutorial, you will learn JavaScript continue statement with the help of syntax and examples.
JavaScript Continue Statement
- JavaScript
continue
statement - Syntax of JavaScript
continue
statement - Example 1:
continue
statement with for loop - Example 2:
continue
statement with while loop - Example 3:
continue
statement with do while loop - Example 4: Using
continue
statement to exit nested loop
JavaScript continue
statement
The javascript continue
statement skipped the execution in programs/scripts.
Syntax of JavaScript continue
statement
continue;
Example 1: continue
statement with for loop
See the following example of continue
statement with for loop:
<script> var i = 1; for (i; i <= 10; i++) { if (i % 5 == 0) { continue; } document.write(i + "<br>") } </script>
The output of the script shown below:
1 2 3 4 6 7 8 9
How it works:
- First, declare a variable
i
and initialize it to 1. - Next, specified test condition.
- After that, Increase the value of
i
by 1 in each iteration of the loop. - Use if statement with continue statement to skipped iteration according to given condition.
- Next, print the value of
i
.
In this above example, the for
loop increments the variable i from 1 to 10. In the body of the loop, the if
statement checks if i is variable divisible by 5 in each iteration of the loop. If so, the continue
statement is executed and the loop is skipped iteration.
Example 2: continue
statement with while loop
See the following example of continue
statement with while loop:
<script> var i = 1; while(i <= 10) { if (i % 5 == 0) { continue; } document.write(i + "<br>") } </script>
The output of the script shown below:
1 2 3 4 6 7 8 9
Example 3: continue
statement with do while loop
See the following example of continue
statement with do-while loop:
<script> var i = 1; do { i++ if (i % 5 == 0) { continue; } document.write(i + "<br>") } while(i <= 10) </script>
The output of the script shown below:
1 2 3 4 6 7 8 9 11
Example 4: Using continue
statement to exit nested loop
You use the continue
statement to skipped a label statement and transfer control to the next statement following the skipped statement.
The syntax is as follows:
continue label;
The continue
statement is typically used to exit the nested loop. See the following example.
<script> // continue with a label outer: for (let i = 1; i <= 3; i++) { for (let j = 1; j <= 3; j++) { if ((i == 2) && (j == 2)) { document.write("<br> continue to outer" + "<br>") continue outer; } document.write("[i:" + i + ",j:" + j + "]") } } </script>
The output of the script shown below:
[i:1,j:1][i:1,j:2][i:1,j:3][i:2,j:1]
continue to outer
[i:3,j:1][i:3,j:2][i:3,j:3]
Here,
- First, the
for
loops increment the variablei
andj
from 1 to 3. - Second, inside the body of the innermost loop, we check if both
i
andj
are equal to 2. If so, we output a message to the web page and jump back to theouter
label. Otherwise, we output the values ofi
andj
in each iteration.
Conclusion
In this tutorial, you have learned what is continue statement and how to use the JavaScript continue
statement to control the code execution of for loop and nested loop.