The break and continue control statements in JavaScript

break and continue statement in javascript

Control statements in JavaScript

JavaScript is a powerful web page development programming language in which the repetitive work can be done with the help of loop. When we use loops in our codes, we may come to the stage where we suddenly want to exit the loop structure currently being executed. There may be situations when you want to exit loop without reaching its end or you may want to skip some part of loop.

In order to exit the loop without reaching its end or skipping some part of loop, JavaScript provides you powerful statements like break and continue. These statements are used in your code to come out of the loop or to continue with other statements.

break statement in JavaScript

The break statement in JavaScript is used to break the current looping structure and push the control out of the loop to the next statement coming outside the loop. The flowchart of break statement is shown below:

https://programmingtutorial.in/

Let us now write a simple code by using while loop in which we are going to use break statement.

<html>
   <body>     
      <script type = "text/javascript">
         var i= 1;
         document.write("We will enter the loop here <br/> "); 
         while (i < 20) {
            if (i == 7) {
               break;   // control will go out of loop
               }
            i = i + 1;
            document.write( i + "<br />");
         }         
         document.write("Exiting the loop!<br /> ");
      </script>
   </body>
</html>

It will give result as

We will enter the loop here
2
3
4
5
6
7
Exiting the loop!

Let us now explain above code, the while loop will start checking the value of variable i and will print 2 as first value and then 3, 4, 5, 6,7. When the value of variable increases to 7, an if statement is checked and found true. As the value of i becomes 7, the if condition goes true and the break statement gets executed which pushes the control to the next statement coming out of the loop.

continue statement in JavaScript

The continue statement breaks current iteration if a particular condition occurs in the loop and continues with the rest of the iterations. The continue statement tells the interpreter to immediately start the next iteration of the loop and skip the remaining code block. When a continue statement is encountered, the program flow moves to the loop check expression immediately and if the condition remains true, then it starts the next iteration, otherwise the control comes out of the loop. Let us understand with an example code:

<html>
    <body>     
       <script type = "text/javascript">
          var i= 1;
          document.write("We will enter the loop here <br/> ");
          while (i < 6) {
             i = i + 1;
             if (i == 3) {
                continue;   // control will skip the value of 3
             }
             document.write( i + "<br />");
          }         
          document.write("Exiting the loop!<br /> ");
       </script>
    </body>
 </html>

It results as

We will enter the loop here
2
4
5
6
Exiting the loop!

In the above code, when the value of i became 3, it has been skipped and rest of the values executed.