Continue statement in JavaScript is a similar to the break statement. It breaks the current iteration of loop and transfers control to the restart of the loop with a new iteration.
In other words, continue statement tells the browser to stop the current iteration of loop and to begin a new iteration of loop.
When the continue statement encounters in a loop, subsequent statements in the loop skips (i.e. not executed) at a specified condition, and the control of execution continues with the next repetition of the loop.
Let us see the below figure to understand continue statement in while loop.
As you can observe in the above code, the continue statement does not break out of the loop entirely. It just jumps back to the beginning of the loop by skipping the rest of code in the loop body for the next iteration.
Syntax of Continue Statement in JavaScript
The syntax of continue statement is just as simple as break statement’s:
continue;
Here, continue is a keyword. From JavaScript 1.2, we can also use continue statement with label to interrupt the loop. The syntax is as:
continue labelname;
Just like break keyword, we write continue keyword inside the statement block that the loop executes, preceded by a conditional test.
Use of Continue in JavaScript
We can use the continue statement (whether labeled or unlabeled) within the block of a while, do/while, for, or for/in loop. Using it anywhere else, it generates a syntax error.
When the continue statement in JavaScript executes, the current iteration of loop terminates and the next iteration starts.
1. If you use the continue statement within a for loop, the control will jump back to its increment expression, execute it, and then check again to determine if another iteration should continue.
2. If used within a for/in loop, then the loop starts over with the next field and continues looping from there.
3. If used within the while or do-while loop, the specified expression at the starting of loop evaluates again. If it is true, the loop body executes beginning from the top.
The use of continue statement in different loops has shown in the below figure.
Let’s understand the use of continue statement in JavaScript with the help of an example program. Suppose we want to display numbers in descending order from 1 to 10. For this, let’s write a JavaScript program using for loop.
Program code:
<html> <head> <title>Use of continue statement</title> </head> <body> <script> for(let i = 5; i >= 1; i--) { document.write(i+ "<br>"); } </script> </body> </html>
Output: 5 4 3 2 1
In this example, the value of i begins with 10 and decrements by 1 until the value of i is greater or equal to 1. Now, we will add continue statement in this program like this:
<html> <head> <title>Use of continue statement</title> </head> <body> <script> for(let i = 5; i >= 1; i--) { if(i > 3) continue; // It will skip the rest statements and go back in the for loop. document.write(i + "<br>"); } </script> </body> </html>
Output: 3 2 1
In this script, when i > 3, we are redirecting the flow of execution back to the next iteration of loop. During the change of the value of i from 5 to 4, the continue statement will execute and subsequent statements will not execute.
As a result, the value of i from 5 to 4 will not print. So, the output will be as above.
Continue Statement Example Programs
Let’s take some important example programs based on the continue statement in JavaScript.
Program code 1: Continue statement inside for loop.
<html> <head> <title>Use of continue statement in for loop</title> </head> <body> <script> // Displaying numbers using continue statement. for(let i = 1; i <= 6; i++) { if(i == 3) { document.write("Continue without 3", "<br>"); continue; } document.write(i + "<br>"); } </script> </body> </html>
Output: 1 2 Continue without 3 4 5 6
As you can see in the output, 3 is not displayed on the browser because the loop is continued when it is equal to 3.
Program code 2: Continue statement inside while loop
<html> <head> <title>Use of continue statement in while loop</title> </head> <body> <script> // Displaying numbers using continue statement. // while loop. let x = 1; // Initialization. while(x <= 6) { if(x == 3){ x++; continue; } document.write(x, "<br>"); x++; } </script> </body> </html>
Output: 1 2 4 5 6
As you can see in the output, the number 3 is skipped.
Program code 3: Continue statement within do-while loop
<html> <head> <title>Use of continue statement in do-while loop</title> </head> <body> <script> // do-while loop. let x = 1; // Declaration and Initialization of variable. do { if(x == 4) { x++; continue; } document.write(x, "<br>"); x++; } while(x <= 6); </script> </body> </html>
Output: 1 2 3 5 6
Program code 4: Continue statement in inner loop
<html> <head> <title>Using of continue statement in inner loop</title> </head> <body> <script> // Outer loop. for(let i = 1; i<= 3; i++) { // Inner loop. for(let j = 1; j <= 3; j++) { if(i == 2 && j == 3) continue; // continue statement inside inner loop. document.write(i+ " " +j, "<br>"); } } </script> </body> </html>
Output: 1 1 1 2 1 3 2 1 2 2 3 1 3 2 3 3
More Examples on Continue Statement
Program code 5:
<html> <head> <title>More continue statement example </title> </head> <body> <script> let i; for(i = 0; i <= 6; i++) { if(i < 2) continue; document.write(i, "<br>"); if(i < 4) continue; document.write(10 * i, "<br>"); } </script> </body> </html>
Output: 2 3 4 40 5 50 6 60
In this script code, we have created a loop that iterates the value of variable i from 0 to 6. If i is less than 2, we do nothing. Once i is 3 or higher, we print its value. Once i is greater than 5, we print 10 * i. So, the above script code will print the values as shown in the output.
Program code 6: Continue statement with label
<html> <head> <title>Continue statement with label </title> </head> <body> <script> let i, j; outerloop: for(i = 0; i <= 3; i++) { innerloop: for(j = 0; j <= 3; j++) { if((i == 2) && (j == 2)) { continue outerloop; // skips when both expressions are true. } else { document.write(i+ ", " +j, "<br>"); } } } </script> </body> </html>
Output: 0, 0 0, 1 0, 2 0, 3 1, 0 1, 1 1, 2 1, 3 2, 0 2, 1 3, 0 3, 1 3, 2 3, 3
In this tutorial, you learned the continue statement in JavaScript with various example programs. Hope that you will have understood how to use continue statement in JavaScript program.
Thanks for reading!!!
Next ⇒ Function in JavaScript⇐ PrevNext ⇒