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.

Continue with a for loop

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

Continue statement in nested loops

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

To exit from the outer loop, we make use of the labeled 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

References

  1. Break
  2. Label
  3. Continue

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top