User Guide

switch 211
switch
Availability
Flash Player 4.
Usage
switch (expression){
caseClause:
[defaultClause:]
}
Parameters
expression
Any expression.
caseClause A case keyword followed by an expression, a colon, and a group of statements to
execute if the expression matches the switch
expression parameter using strict equality (===).
defaultClause A default keyword followed by statements to execute if none of the case
expressions match the switch
expression parameter strict equality (===).
Returns
Nothing.
Description
Statement; creates a branching structure for ActionScript statements. As with the if statement,
the
switch statement tests a condition and executes statements if the condition returns a value of
true
. All switch statements should include a default case. The default case should include a break
statement that prevents a fall-through error if another case is added later. When a case falls
through, it doesnt have a break statement.
Example
In the following example, if the String.fromCharCode(Key.getAscii()) parameter evaluates
to
A, the trace() statement that follows case "A" executes; if the parameter evaluates to a, the
trace() statement that follows case "a" executes; and so on. If no case expression matches the
String.fromCharCode(Key.getAscii()) parameter, the trace() statement that follows the
default keyword executes.
var listenerObj:Object = new Object();
listenerObj.onKeyDown = function() {
switch (String.fromCharCode(Key.getAscii())) {
case "A" :
trace("you pressed A");
break;
case "a" :
trace("you pressed a");
break;
case "E" :
case "e" :
trace("you pressed E or e");
break;
case "I" :
CHAPTER 5
ActionScript Core Language Elements