jump to navigation

Using the Switch Conditional in ActionScript 3.0 September 22, 2010

Posted by Tournas Dimitrios in Actionscript.

This tutorial will teach you how to use the Switch statement to be able to execute different codes depending on the value of a specified variable. This is useful for situations where a Flash application is required to take different actions in accordance with a specific circumstance. For example, say you have a website that shows a different background depending on the day of the week. The switch statement allows you to put your code in a format that will execute a specific code depending on the value of a day variable.

Structure of the Switch Statement :

The switch statement requires a variable and a case value for which the different actions will be specified. It is written in the following generalized code:

switch (myVariable){

case 1:

case 2:

case 3:


In essence, switch statement is a conditional similar to the if conditional. The statement will only execute a code if one of the conditions is satisfied.

Basic Usage – Single Case :

The switch conditional is more commonly used with a large number of cases, but it can be used as a single conditional that will only execute the code if the variable provided matches the case in question. For example, the code below will only be executed if the variable month equals “December”:

switch (month){

case "December":
trace("Yay, we are in the last month of the year!");

Such use is superficial for a switch statement because the same result could be achieved by using an if conditional in a shorter format. However, the power of a switch statement is seen when there is more than one case.

Multiple Cases :

It is more likely to use a switch statement with multiple cases so that the a specific code would only be executed for appropriate cases only. In the example code below the window will output a message in one of the designated languages depending on the value of the variable language:

switch (language){

case "English":

case "Arabic":

case "Spanish":


The code to be executed for each case must end with a break statement, such a statement informs Flash that this is the end of the assigned code, omitting it might not generate any compile time errors, but would lead to a confusing result during playback. The last case does not technically need a break statement, but it is always put for the sake of consistency.

This concludes our tutorial on the switch statement, I hope that you found this tutorial helpful.



No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s