[Contents] [Previous] [Next] [Index]

break

Terminates the current while or for loop and transfers program control to the statement following the terminated loop.

Implemented in

Navigator 2.0, LiveWire 1.0

Syntax

break
break label

Argument

label
Identifier associated with the label of the statement.

Description

The break statement can now include an optional label that allows the program to break out of a labeled statement. This type of break must be in a statement identified by the label used by break.

The statements in a labeled statement can be of any type.

Examples

The following function has a break statement that terminates the while loop when e is 3, and then returns the value 3 * x.

function testBreak(x) {
   var i = 0
   while (i < 6) {
      if (i == 3)
         break
      i++
   }
   return i*x
}
In the following example, a statement labeled checkiandj contains a statement labeled checkj. If break is encountered, the program breaks out of the checkj statement and continues with the remainder of the checkiandj statement. If break had a label of checkiandj, the program would break out of the checkiandj statement and continue at the statement following checkiandj.

checkiandj : 
   if (4==i) {
      document.write("You've entered " + i + ".<BR>");
      checkj :
         if (2==j) {
            document.write("You've entered " + j + ".<BR>");
            break checkj;
            document.write("The sum is " + (i+j) + ".<BR>");
         }
      document.write(i + "-" + j + "=" + (i-j) + ".<BR>");
   }

See also

labeled, switch


[Contents] [Previous] [Next] [Index]

Last Updated: 10/31/97 12:29:59


Copyright © 1997 Netscape Communications Corporation