AS3 switch/case statement syntax
This howto describes the basic usage of switch/case statements in actionscript.
Case/Switch statements are frequently used for complex if/then coding. Here is a basic example of how to build these constructs.
switch (example_string)
{
case "a":
trace ("a was selected");
break;
case "b":
trace ("b was selected");
break;
default:
trace ("Neither a or b was selected")
}
By leaving out the break, you can create the same action for several different case statements…
switch (example_string)
{
case "a":
case "b":
trace ("either a or b was selected");
break;
default:
trace ("Neither a or b was selected")
}










Name said on December 26, 2009
indenting?
irritated said on June 16, 2010
Stupid AS3.
If (function())
irritated said on June 16, 2010
Stupid me. (it’s been a long day)
If (function())
lolwut? said on July 20, 2010
so is it cool that there is no ; at the end of the line in the default statement?
American said on August 15, 2010
good tut, simple and to the point. I hate it when people post beginner tuts and end up adding WAY more info than anybody needs or whats to know. This was refreshing.
Liamsteele said on September 5, 2010
yes, rather than using a ; to signify an end of line, it uses a : to signify that the next section is different.
the : and break; act similarly to curly brackets in functions.
this description might not be 100% technically accurate, but it works ;D
Rubixchick said on March 8, 2011
I have a question. So is break then a replacement for using if/else? Like would the alternative to this be, “If a is selected then whatever” else if “b is selected, whatever” else if neither a or b is selected, this happens instead.
Mat said on March 24, 2011
bahu mog ilong
Mat said on March 24, 2011
kawatan mog manok
tugBUT said on July 22, 2011
How could I tell the switch to perform a action when a value is false? For instance:
case != 37:
blah blah
Any pre-built way to do this???
Tarek said on October 30, 2011
@tugBut: You might certainly have figured it out already, but yes there is a way. Although it’s easier to use if/then when you don’t have to do anything when the value is 37, like the example below:
if (value != 37) {
// your code
}
Otherwise, use this:
switch (your_number) {
case 37:
// your code for when your_number value = 37
break;
default:
// your code for all other values
}