home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

JavaScript: The Definitive GuideJavaScript: The Definitive GuideSearch this book

6.11. break

The break statement causes the innermost enclosing loop or a switch statement to exit immediately. Its syntax is simple:

break; 

Because it causes a loop or switch to exit, this form of the break statement is legal only if it appears within one of these statements.

ECMAScript v3 and JavaScript 1.2 allow the break keyword to be followed by the name of a label:

break labelname; 

Note that labelname is simply an identifier; it is not followed by a colon, as it would be when defining a labeled statement.

When break is used with a label, it jumps to the end of, or terminates, the named statement, which may be any enclosing statement. The named statement need not be a loop or switch; a break statement used with a label need not even be contained within a loop or switch. The only restriction on the label of the break statement is that it name an enclosing statement. The label can name an if statement, for example, or even a block of statements grouped within curly braces, for the sole purpose of naming the block with a label.

As discussed in Chapter 2, a newline is not allowed between the break keyword and the labelname. This is an oddity of JavaScript syntax caused by its automatic insertion of omitted semicolons. If you break a line of code between the break keyword and the following label, JavaScript assumes you meant to use the simple, unlabeled form of the statement and adds a semicolon for you.

We've already seen examples of the break statement within a switch statement. In loops, it is typically used to exit prematurely when, for whatever reason, there is no longer any need to complete the loop. When a loop has complex termination conditions, it is often easier to implement some of these conditions with break statements, rather than trying to express them all in a single loop expression.

The following code searches the elements of an array for a particular value. The loop terminates naturally when it reaches the end of the array; it terminates with a break statement if it finds what it is looking for in the array:

for(i = 0; i < a.length; i++) {
    if (a[i] == target)
        break;
} 

You need the labeled form of the break statement only when you are using nested loops or switch statements and need to break out of a statement that is not the innermost one.

The following example shows labeled for loops and labeled break statements. See if you can figure out what its output will be:

outerloop:
  for(var i = 0; i < 10; i++) {
    innerloop:
      for(var j = 0; j < 10; 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("i = " + i + " j = " + j + "<br>");
      }
  }
  document.write("FINAL i = " + i + " j = " + j + "<br>"); 


Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.











??????????????@Mail.ru