Written by Philip
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");
}
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("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(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");
}
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");
}
Note that the default keyword does not need to be used.| Related Articles | Link |
| Wikipedia Switch Statements |
http://en.wikipedia.org/wiki/Switch_statement |