Computer >> Computer tutorials >  >> Programming >> Javascript

Describe JavaScript Break, Continue and Label Statements


break statement

The break statement is used to exit a loop early, breaking out of the enclosing curly braces. The break statement exits out of a loop.

Let’s see an example of break statement in JavaScript. The following example illustrates the use of a break statement with a while loop. Notice how the loop breaks out early once x reaches 5 and reaches to document.write (..) statement just below to the closing curly brace

Example

Live Demo

<html>
   <body>
      <script>
         var x = 1;
         document.write("Entering the loop<br /> ");

         while (x < 20) {
            if (x == 5) {
               break; // breaks out of loop completely
            }
            x = x + 1;
            document.write( x + "<br />");
         }
         document.write("Exiting the loop!<br /> ");
      </script>
   </body>
</html>

continue statement

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.
The continue statement breaks over one iteration in the loop. This example illustrates the use of a continue statement with a while loop. Notice how to continue statement is used to skip printing when the index held in variable x reaches 8

Example

Live Demo

<html>
   <body>
      <script>
         var x = 1;
         document.write("Entering the loop<br /> ");

         while (x < 10) {
            x = x + 1;
            if (x == 8) {
               continue; // skip rest of the loop body
            }
            document.write( x + "<br />");
         }
         document.write("Exiting the loop!<br /> ");
      </script>
   </body>
</html>

Label statement

JavaScript label statements are used to prefix a label to an identifier. A label can be used with break and continue statement to control the flow more precisely. A label is simply an identifier followed by a colon (:) that is applied to a statement or a block of code. We will see two different examples to understand how to use labels with break and continue.
You can try to run the following code to use labels to control the flow, with break statement

Example

Live Demo

<html>
   <body>
      <script>
         document.write("Entering the loop!<br /> ");
         outerloop: // This is the label name

         for (var i = 0; i < 5; i++) {
            document.write("Outerloop: " + i + "<br />");
            innerloop:
               for (var j = 0; j < 5; j++) {
                  if (j > 3 ) break ; // Quit the innermost loop
                  if (i == 2) break innerloop; // Do the same thing
                  if (i == 4) break outerloop; // Quit the outer loop
                  document.write("Innerloop: " + j + " <br />");
               }
         }
         document.write("Exiting the loop!<br /> ");
      </script>
   </body>
</html>