AS3 switch/case statement syntax
Posted by David Kirk in Computer programming
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")
}
About David Kirk
View more articles by David Kirk
The Conversation
Follow the reactions below and share your own thoughts.





December 26, 2009 at 4:38 am, Name said:
indenting?
June 16, 2010 at 3:50 pm, irritated said:
Stupid AS3.
If (function())
June 16, 2010 at 3:52 pm, irritated said:
Stupid me. (it’s been a long day)
If (function())
July 20, 2010 at 3:09 am, lolwut? said:
so is it cool that there is no ; at the end of the line in the default statement?
September 05, 2010 at 12:28 am, Liamsteele said:
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
August 15, 2010 at 3:14 pm, American said:
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.
March 08, 2011 at 4:31 pm, Rubixchick said:
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.
March 24, 2011 at 3:23 am, Mat said:
bahu mog ilong
March 24, 2011 at 3:25 am, Mat said:
kawatan mog manok
July 22, 2011 at 2:52 am, tugBUT said:
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???
October 30, 2011 at 12:25 pm, Tarek said:
@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
}
November 15, 2012 at 11:37 am, Frederick said:
I have a small problem with the case statement. In the snippet target is – public var target:DisplayObject;
The detection works fine but how to pass correctly to get the switch statement working?
if (theRoller.hitTestObject(target))
{
trace(this["target"]); //[object Erikpl]
switch (this["target"])
{
case “[object Erikpl]“:
trace(” RightHit”,target);
break;
default:
//trace (“Nothing was selected, solve the problem”);
}
}