The AS3 Switch Case can of course be used with Flash, Flex or AIR. As all of these are programmed with AS3 (Action Script 3), which is a strongly typed object oriented language.

Like other languages the AS3 switch case allows you to compare a value, to a number of different values. If the value you are comparing matches one of the values you are comparing with then you can execute a certain piece of code, who knows what, that is up to you!

Below are AS3 switch case Examples. Firstly I’ve shown the AS3 switch case’s syntax, then the basic syntax without the break keyword, and then two switch case examples, one using strings, and the second switch case example using integers.

AS3 Switch case syntax

switch(condition)
{
  case condition 1:
  trace("Condition 1 was met");
  break;
  case condition 2:
  trace("Condition 2 was met");
  break;
  case condition 3:
  trace("Condition 3 was met");
  break;
  default:
  trace("None of the above were met");
}

Switch case syntax without the break keyword Now that you know the basic syntax you also need to know that you can leave out the break within the as3 switch case code.

switch(condition)
{
  case condition 1:
  case condition 2:
  trace("Condition 1 or 2 was met");
  break;
  case condition 3:
  trace("Condition 3 was met");
  break;
  default:
  trace("None of the above were met");
}

In the example above if the condition 1 or 2 is met then the code executes and traces out “Condition 1 or 2 was met”. Now lets move onto some examples. Switch case using strings.

switch("hello world")
{
  case "hello":
  trace("The man says hello");
  break;
  case "hello cat":
  trace("The man says hello cat");
  break;
  case "hello world":
  trace("The man says hello world");
  break;
  default:
  trace("None of the above were met");
}

In this example the text “The man says hello world” is traced out. Switch Case using integers, what fun.

switch(1)
{
  case 1:
  trace("The Number 1");
  break;
  case 2:
  case 3:
  trace("The Number 2 or 3");
  break;
  default:
  trace("None of the numbers above");
}

In the example above the text “The Number 1” was traced out. Switch Case using integers without the break keyword!

switch(2)
{
  case 1:
  trace("The Number 1");
  case 2:
  case 3:
  trace("The Number 2 or 3");
  break;
  default:
  trace("None of the numbers above");
}

In the example above the text “The Number 2 or 3” was traced out.

Note that the default keyword does not need to be used.

When your happy with your switch case statements, replace the trace statements with your own code, and enjoy using the as3 switch cases.

Related Articles Link
Wikipedia Switch Statements http://en.wikipedia.org/wiki/Switch_statement