User Guide
Conditionals 87
switch
The switch statement is useful if you have several execution paths that depend on the same
condition expression. It provides functionality similar to a long series of
if..else if
statements, but is somewhat easier to read. Instead of testing a condition for a Boolean value,
the
switch statement evaluates an expression and uses the result to determine which block of
code to execute. Blocks of code begin with a
case statement, and end with a break statement.
For example, the following
switch statement prints the day of the week based on the day
number returned by the
Date.getDay() method:
var someDate:Date = new Date();
var dayNum:uint = someDate.getDay();
switch(dayNum)
{
case 0:
trace("Sunday");
break;
case 1:
trace("Monday");
break;
case 2:
trace("Tuesday");
break;
case 3:
trace("Wednesday");
break;
case 4:
trace("Thursday");
break;
case 5:
trace("Friday");
break;
case 6:
trace("Saturday");
break;
default:
trace("Out of range");
break;
}