The switch Statement


The switch Statement

The switch statement first appeared in the Netscape Navigator version 3.0, and then in the Internet Explorer 4.0, as you see in Table 2.11.

Table 2.11. The switch Statement

Statement

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

switch

 

x

x

x

   

x

x

x

x

This statement enables you to evaluate an expression and match the expression's value to a number of test values, executing code when you find a match. You place the expression to evaluate in parentheses after the switch keyword, and then give the test values you want to match in a set of case statements. If you match a test expression, the code in that case statement is executed up to a break statement (which ends the switch statement). Here's how it all looks formally :

 switch (  expression  ){      case  label1  :  statements  break      case  label2  :  statements  break          .          .          .      [default:  statements  ]  } 

Note the optional default statement at the end. If the value of the expression doesn't match any test value, the code in the default statement, if there is one, is executed. If you include a default statement, it should be the last statement, following all the case statements.

As with other programming constructs, this is all much more easily seen in an example. In this next example, I'll check the text in a variable named command , using a switch statement:

(Listing 02-16.html on the web site)
 <HTML>      <HEAD>          <TITLE>              Working With the switch Statement          </TITLE>      </HEAD>      <BODY>          <H1>Working With the switch Statement</H1>          <SCRIPT LANGUAGE="JavaScript">          <!--  var command = "PANIC!"   switch (command) {   case "right":   document.write("Go right.")   break   case "left":   document.write("Go left.")   break   case "up" :   document.write("Go up.")   break   case "down":   document.write("Go down.")   break   case "PANIC!":   document.write("Head for the hills!")   break   default:   document.write("Sorry, I did not understand.")  }          // -->          </SCRIPT>       </BODY>  </HTML> 

As you can see in Figure 2.14, the test value "PANIC!" matched, and the code wrote out the corresponding message.

Figure 2.14. Using the switch statement.

graphics/02fig14.gif

Note that you can match more than strings in switch statementsfor example, you can use numbers as test values. In fact, you can mix numbers and strings like this example, which displays the text Go left. :

 var command = 5  switch (command) {      case "right":          document.write("Go right.")          break      case 5:          document.write("Go left.")          break      case "up":          document.write("Go up.")          break      case 12:          document.write("Go down.")          break      case 9:          document.write("Head for the hills!")          break      default:          document.write("Sorry, I did not understand.")  } 

Tip

If the break statement is omitted in a case statement, the program continues, executing the code in the next case statement. That can be useful to know in some cases, where you want to execute the following case statement(s) as well as the current one ( sort of like singing "The Twelve Days of Christmas," where "five golden rings" falls through to "four calling birds" and so on). The usual way to write switch statements, however, is to end each case statement with a break statement. (The default statement, being the last statement, doesn't need a break statement, although you can include one if you prefer.)


That completes our chapter on data, operators, and branching. Now we can take the next step as we turn to loops , functions, and more in the next chapter.



Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net