We had covered Break and Continue statements earlier. But let us revisit it again to strengthen our understanding.

Javascript Break Statement





A javascript break statement is used to stop the normal execution of a loop. The loop can be a for loop or a while loop. Let us take a look at for loop, and see how we can stop its normal execution. Take a look at the following example where we wish to break the execution of the for loop when value of i reaches 5. Normaly the loop would have executed till the value of i reaches to 10.

  1. <html>
  2. <body>
  3. <script type="text/javascript">
  4. <!--
  5. /*
  6. ********************************************************
  7. Example break statement in a for loop
  8. ReferenceDesigner.com javascript tutorial
  9. ********************************************************
  10. */
  11.  
  12. var i;
  13. var cm ;
  14. // Print a Table of Inch Centimeter
  15. document.write("Inch cm <br />");
  16. for (i=1; i<=10; i++)
  17. {
  18. cm = i*2.54 ;
  19. document.write( i );
  20. document.write(" "+cm+"<br />");
  21. if (i == 5) break;
  22. }
  23.  
  24. //-->
  25. </script>
  26. </body>
  27. </html>


This code produces the following output.


Inch cm
1 2.54
2 5.08
3 7.62
4 10.16
5 12.7




You may like to try this example online here

The break statement can also be used inside a while loop. Here is the same example presented to show the use of break statement in a while loop.

  1. <html>
  2. <body>
  3. <script type="text/javascript">
  4. <!--
  5. /*
  6. ********************************************************
  7. Example break statement in a while loop
  8. ReferenceDesigner.com javascript tutorial
  9. ********************************************************
  10. */
  11.  
  12. var i;
  13. var cm ;
  14. i=1;
  15. // Print a Table of Inch Centimeter
  16. document.write("Inch cm <br />");
  17. while (i<=10)
  18. {
  19. cm = i*2.54 ;
  20. document.write( i );
  21. document.write(" "+cm +"<br />" );
  22. if (i == 5) break;
  23. i++;
  24. }
  25.  
  26. //-->
  27. </script>
  28. </body>
  29. </html>




You may like to try this example online here

Continue Statement in a for loop


The continue statement in a for loop keyword causes flow of control to immediately jump to the update statement. The continue statement is helpful if we want to stop the normal flow of the control return to the loop without executing the statements WRITTEN AFTER the continue statement.

  1. <html>
  2. <body>
  3. <script type="text/javascript">
  4. <!--
  5. /*
  6. ********************************************************
  7. Example continue statement in a for loop
  8. ReferenceDesigner.com javascript tutorial
  9. ********************************************************
  10. */
  11. var i;
  12. var num3 = 0;
  13.  
  14. for ( i=1; i<=20; i++)
  15. {
  16. if ( i%3 == 0) continue ;
  17. num3++;
  18. }
  19. document.write("Number of numbers not divisible by 3 between 1 and 20 is "+ num3);
  20. //-->
  21. </script>
  22. </body>
  23. </html>


Continue Statement in a while loop


The continue statement in a while or do while loop, causes the flow of control to immediately jumps to the Boolean expression. See the example below.

  1. <html>
  2. <body>
  3. <script type="text/javascript">
  4. <!--
  5. /*
  6. ********************************************************
  7. Example continue statement in a while loop
  8. ReferenceDesigner.com javascript tutorial
  9. ********************************************************
  10. */
  11. var i=1;
  12. var num3 = 0;
  13. while( i<=20 ) {
  14. i++;
  15. if ( i%3 == 0) continue ;
  16. num3++;
  17. }
  18. document.write("Number of numbers not divisible by 3 between 1 and 20 is "+ num3);
  19. //-->
  20. </script>
  21. </body>
  22. </html>