Quick References
      ADO
      ASP
      CSS2
      HTML
      JavaScript
      Jet SQL
      VBScript
      WML
      WMLScript
      WSH
      XHTML
      XML DOM
      XSLT

Features
      Knowledge Base
      Tutorials

Partners
     ZVON.ORG
     XML
     Planet Source Code
     VisualBuilder
     Web Design
     Your HTML Source
     XML/XSLT Forums
     ASPAlliance
     Scripts
     
     Programmers Heaven
     Tek-Tips Forums
     Developer Fusion
     Code Project











STATEMENT: break


break [ label ]
 
The break statement can be used to terminate a current loop, switch or label statement and pass control to the statement immediately following it.
 
The following example starts a loop printing the numbers from 1 to 10, buts exits when it reaches 7 with an appropriate message:
 
Code:
var i = 0
while (i < 10)
{
   document.write(i);
   if (i==7)
   {
      document.write("the counter has reached " + i);
      break;
   }
   i++;
}

 
The break statement can also be used with a label as in the following example of two counts, one nested within the other, which will be ended if the inner counter variable is equal to the variable 'x':
 
Code:
outer_loop:
for(i=0; i<3; i++)
{
   document.write("<BR>" + "outer " + i + ":   ");
   for(j=0; j<5; j++)
   {
      document.write("inner " + j + " ");
      if(j==x)
         break outer_loop;
   }
}

 
While the break statement on its own can only be used to exit a loop, the optional label can be added to break to exit any kind of statement. This next example tests for an even number and, whenever it finds one, displays it, unless that number is 12:
 
Code:
even_number:
if(i%2==0)
{
   if(i==12)
      break even_number;
   document.write(i);
}

 


Copyright 1999-2005 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information