JavaScript Continue Statement Example

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  iand 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 variable i and j from 1 to 3.
  • Second, inside the body of the innermost loop, we check if both i and j are equal to 2. If so, we output a message to the web page and jump back to the outer label. Otherwise, we output the values of i and j 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.

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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