Table of Contents

7.4 The switch Statement

The switch statement, introduced in Flash Player 6, lets us execute one (or sometimes more) of several possible code blocks, based on the value of a single test expression. In some cases, the switch statement is easier to write and more legible than an if statement. For example, in the following switch statement, we greet the user with a custom message depending on the value of the test expression gender:

var surname = "Porter";
var gender = "male";
switch (gender) {
  case "femaleMarried":
    trace("Hello Mrs. " + surname); 
    break;
  case "femaleGeneric":
    trace("Hello Ms. " + surname);
    break;
  case "male":
    trace("Hello Mr. " + surname);
    break;
  default:
    trace("Hello " + surname);
}

Our switch statement attempts to match the value of gender to one of the supplied case expressions: "femaleMarried", "femaleGeneric", or "male". Because gender matches the expression "male", the substatement trace("Hello Mr. " + surname); is executed. If the test expression had not matched any case clause, then the default statement—trace("Hello" + surname);—would have been executed.

The general form of the switch statement is:

switch (testExpression) {
  case expression1:
    substatements1
    break;
  case expression2:
    substatements2
    break;
  default:
    substatements3
    break;
}

where testExpression is a value that the interpreter will attempt to match with each of the supplied case expressions, from top to bottom. The case expressions are supplied with the statement label case, followed by a colon. If testExpression matches a case expression, all statements immediately following that case label are executed, including those in any subsequent case blocks! To prevent subsequent blocks from executing, we must use the break statement at the end of each block. If no case expression matches testExpression, all statements following the default label are executed.

Though the default label is normally listed last, it can legally come anywhere within the switch statement. Furthermore, the default label is not mandatory in a switch statement. If no default is provided and testExpression does not match a case expression, execution flow simply continues after the end of the switch statement block (the code within the switch statement is skipped). However, if both the case and the default labels are omitted, the switch statement generates an error (unlike JavaScript, which permits empty switch statements).

To provide switch with value ranges rather than a single value, use the following technique:

switch (true) {
  case (x < 10):
    trace("x is less than 10.");
    break;
  case (x >= 10 && x <= 20):
    trace("x is between 10 and 20."); 
    break;
  case (x > 20):
    trace("x is greater than 20.");
    break;
}

In this example, note that if more than one case yields true, the code following the first true expression is executed.

To test for a group of cases rather than a single case, use the following "fall-through" technique (notice how the break statement is intentionally omitted from case 1 and case 3:

x = 3;
   
switch (x) {
  case 1:
  case 3:
  case 5:
    trace("x is 1, 3, or 5");
    break;
  default:
    trace("x is not 1, 3, or 5");
}

The switch statement implicitly uses the strict equality operator (= = =), not the equality operator (= =), when comparing the testExpression with case expressions. For a description of the difference, see Chapter 5. The expressions following the switch and case keywords can be any datatype and need not be literal values. Because a switch statement executes from top to bottom, case expressions that are most likely to match testExpression should be listed first (obviously, this applies only to situations where case statement order does not affect the result of the switch statement).

Although the switch statement was introduced for Flash Player 6, code using switch can be exported from the Flash MX authoring tool to Flash Player 5 and Flash Player 4 formatted movies. When exporting to Flash 4 or Flash 5 format, however, switch expression matching uses the standard equality (= =), not the strict equality (= = =) operator. In any case, switch does not offer any performance improvement over the analogous if-else if-else construction.

Note that case and default clauses are two specific examples of a more general structure called a statement label. Statement labels are a part of ECMAScript v3, but—other than case and default—they are not supported by ActionScript.

7.4.1 Simulating the switch Statement in Flash 5

Though the switch statement is not supported in the Flash 5 authoring tool, it can be emulated using a chain of if-else if-else statements, like this:

var surname = "Porter";
var gender = "male";
if (gender =  = "femaleMarried") {
  trace("Hello Mrs. " + surname);
} else if (gender =  = "femaleGeneric") {
  trace("Hello Ms. " + surname);
} else if (gender =  = "male") {
  trace("Hello Mr. " + surname);
} else {
  trace("Hello " + surname);
}

Table of Contents