Continue statement in Typescript

The Typescript Continue statement skips the current iteration immediately and continues to the next iteration of the loop. Unlike the break statement, the continue statement does not exit the loop. It only skips the current iteration.

Syntax

Where the label is optional. We use it when we have a nested loop to correctly identify the loop

Continue with a while loop

In the following while loop example, we use the continue statement to skip the iteration if the value of i is 3. Because of this, the value 3 is not printed on the console. The loop does not stop but continues to the next iteration.

Source Code

Continue with a for loop

In this for loop example also skips the rest of the loop, when the value of i is 3.

Source Code

Continue statement in nested loops

In the following nested for loop example, the continue statement skips the inner loop, but not the outer loop.

Source Code

To exit from the outer loop, we make use of the labelled statement

In the following examples, we label the outer loop as outerloop and inner loop as innerloop. The continue statement now skips the outer loop

Source Code

References

  1. Break
  2. Label
  3. Continue

Read More

Leave a Comment

Your email address will not be published.

Scroll to Top