Js Run for Lopp Again When It Is Complete

totn JavaScript


JavaScript: Continue Statement

This JavaScript tutorial explains how to utilise the keep statement with syntax and examples.

Description

In JavaScript, the continue statement is used when you lot want to restart a new iteration of a loop. This statement can be used in a while loop, for loop or for-in loop. If you try using the proceed statement in other loop types, yous might get unexpected results.

When JavaScript executes the continue argument, any remaining code left in the current iteration of the loop body is skipped. The code will resume execution at the start of the loop (every bit a new iteration of the loop).

You can too use a continue statement to execute a labeled statement.

Syntax

The syntax for the continue statement in JavaScript is:

            continue [label_name];          

Parameters or Arguments

label_name
Optional. An identifier name (or characterization proper noun) for a statement.

Note

  • Y'all use the continue argument to restart a loop such every bit the while loop, for loop or for-in loop.
  • If there are nested loops, the continue argument will restart the innermost loop.

Example

Let'due south wait at an example that shows how to use a continue statement in JavaScript.

How to use the Continue Statement with the While Loop

Y'all can also use the proceed argument to restart a new iteration of the while loop.

For instance:

            var counter = 0;  while (counter < 5) {     counter++;     if (counter == 3) {       continue;    }     console.log(counter + ' - Inside while loop on TechOnTheNet.com'); }  panel.log(counter + ' - Done while loop on TechOnTheNet.com');          

In this example, the continue statement is used to restart a new iteration of the while loop and skip the residual of the loop body.

This case will output the post-obit to the web browser panel log:

1 - Inside while loop on TechOnTheNet.com 2 - Inside while loop on TechOnTheNet.com four - Inside while loop on TechOnTheNet.com v - Inside while loop on TechOnTheNet.com 5 - Done while loop on TechOnTheNet.com

Equally you tin can meet, an entry is non written to the web browser panel log when the counter is equal to 3. The keep argument has restarted the loop before the following command tin be run (but only when the counter is equal to 3):

            console.log(counter + ' - Within while loop on TechOnTheNet.com');          

TIP: Notice that in the above example, we have incremented the counter variable at the tiptop of the while loop with the following command:

              counter++;            

We take done this so as to avoid creating an space loop in our logic. If our counter had been incremented at the end of the loop, then once the counter is equal to 3, it would get "stuck" at a value of 3 and the while loop would never terminate.

How to employ the Continue Statement with the For Loop

You tin can as well use the continue argument to restart a new iteration of the for loop. Let'due south rewrite our example with the for loop.

For instance:

            for (var counter = 1; counter < five; counter++) {     if (counter == three) {       continue;    }    console.log(counter + ' - Within for loop on TechOnTheNet.com'); }  console.log(counter + ' - Done for loop on TechOnTheNet.com');          

In this example, the continue statement is used to restart a new iteration of the for loop and skip the residue of the loop trunk.

This example will output the following to the spider web browser console log:

1 - Inside for loop on TechOnTheNet.com 2 - Inside for loop on TechOnTheNet.com iv - Inside for loop on TechOnTheNet.com 5 - Done for loop on TechOnTheNet.com

In this example, an entry is not written to the spider web browser console log when the counter is equal to 3. The continue statement has restarted the loop before the following control can be run (but only when the counter is equal to 3):

            console.log(counter + ' - Inside for loop on TechOnTheNet.com');          

brentlichannoosee.blogspot.com

Source: https://www.techonthenet.com/js/continue.php

0 Response to "Js Run for Lopp Again When It Is Complete"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel