JavaScript Loop Control – Continue & Break Statement with Example

In our last tutorial, we discussed different types of loops in JavaScript. Here, we will learn JavaScript loop control, in which we will learn JavaScript Break and Continue statements with their examples.

So, without wasting time, let’s explore the JavaScript loop control.

Two types of Loop Control statement in JavaScript

JavaScript Loop Control

There are two ways to control the JavaScript loops:

1. Break Statement in JavaScript

While you are in a loop and you want to exit from the loop when a specified condition is met, in such situation, you can use the break statement in JavaScript.

Example of Break Statement

<html>
</body>
     <script type = “text/javascript”>
           var i = 1;
      while( i<=5)
      {
         If( i==2)
          Break;
      document.write( i + “ number of time <br>”);
        i++;
         }
   </script>
</body>
</html>

In this code, the loop will execute only 1 time because when the loop will meet the condition i ==2, then the break statement will terminate the loop and comes out of the body loop.

You must explore top career opportunities in JavaScript

2. JavaScript Continue Statement

When the JavaScript loop encounters then the loop control goes to the starting point of the loop and skip the remaining code block.

Example of Continue Statement

<html>
<body>
    <script type = “text/javascript”>
        var 1 = 0;
        while ( i< =5)
   {
       i ++;
       if (i ==2)
             continue;
             document.write( i + “number of times <br>”);
}
      </script>
</body>
</html>

The output of the programs:

1 number of time

3 number of time

4 number of time

5 number of time

6 number of time

In the output you can see the 2 is missing because when it meets condition if ( i ==2) inside the loop, it will continue to the starting point of the loop and skip the remaining code block.

How to use labels to control the flow?

The label statements can be used to continue and break statements to control the flow more accurately. A label is simply an identifier followed by a colon(:) that is applicable to a group of statements.

Have a look at JavaScript Switch Case

Example

<html>
<body>
<script type = “text/javascript”>
<! –
document.write( “Enter the loop”);
outerloop;
for ( var =0; i<5; i++)
{
document.write(outerloop: “++i”);
innerloop:
for( var j=0; j<5; j++)
{
If(j>3) break;//quits the innermost loop
If(i==2) breakinner loop; //do the same thing
If(i ==4) breakloop;//quit the outer loop
document.write( “Innerloop: “+j+”);
}
}
document.write (“exiting the loop”);
//-->
</script>
</body>
</html>

Output:

Entering the loop!

Outer loop: 0

Inner loop: 0

Inner loop: 1

Inner loop: 2

Inner loop: 3

Outer loop: 1

Inner loop: 1

Inner loop : 2

Inner loop: 3

Outer loop: 2

Outer loop: 3

Inner loop: 1

Inner loop: 2

Inner loop: 3

Outer loop: 4

Exiting the loop!

Summary

Hence, we have two different ways for JavaScript loop control statements like break and continue statements with their examples. In addition, we discussed how to use labels to control the flow.

Hope, you liked the explanation. you may also like to know different types of operators in JavaScript. For queries and suggestion, refer our comment box.

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.