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")
}

 

About David Kirk

David Kirk is one of the original founders of tech-recipes and is currently serving as editor-in-chief. Not only has he been crafting tutorials for over ten years, but in his other life he also enjoys taking care of critically ill patients as an ICU physician.
View more articles by David Kirk

The Conversation

Follow the reactions below and share your own thoughts.

  • Name

    indenting?

  • irritated

    Stupid AS3.

    If (function())

    • irritated

      Stupid me. (it’s been a long day)

      If (function())

  • lolwut?

    so is it cool that there is no ; at the end of the line in the default statement?

    • Liamsteele

      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

  • American

    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.

  • Rubixchick

    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

    bahu mog ilong

  • Mat

    kawatan mog manok

  • tugBUT

    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

    @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
    }

  • Frederick

    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”);
    }
    }