Conditional Statements: Switch Versus If/Else if/Else

You will often need to make a decision in your ActionScript, choosing to do one thing under one situation, and another thing under a different situation. The use of conditionals is a great way to accomplish this and can very well be your most used ActionScript solution. However, how you use your conditionals will make a big difference on how your ActionScript is read by others or even as you revisit it a month from now.

What are conditional statements?

With the use of conditional statements, you can simply test to see whether if an action occurred or is met, then you can execute the following ActionScript. If that condition is not met, you can either take an action or chose to do nothing. I am not sure if that will totally get the concept across but an example will definitely help you understand it a bit more.

Two ways on going about using conditional statements is with the use of the If/Else if/Else statement and the Case and Switch statement.

Using if/else if/else

if (color=="blue") {
 trace("The color is blue");
} else if (color=="red") {
 trace("The color is red");
} else if (color=="green") {
 trace("The color is green");
} else {
 trace("The color is black");
}


As you see a color is being checked for and it will continue checking until a color is matched. If there is not a matching color then it will go with the color “black”. While this will get the job done, as you add more colors and more actions within each check, it can get pretty messy and hard to read as your project grows. Now lets take a look at the same scenario using a case and switch statement.

Using case and switch

switch (color) {
 case "blue" :
 trace("The color is blue");
 break;
 case "red" :
 trace("The color is red");
 break;
 case "green" :
 trace("The color is green");
 break;
 default :
 trace("The color is black");
}

With the use of a case and switch statement, you can easily determine what action needs to be met. (color) Every possible value of the color is then checked until the action has been met. Also, the addition of the break() makes switch an efficient alternative to a more complex series of multiple if statements. Each break line prevents any following instructions from executing. Overall, it is a much easier way to read a longer conditional statements.

Switch vs If/Else if: Which one do I use?

There really is no “right or wrong” way to use Switch versus If/Else if statements. However, I guess the best advice is to consider ActionScript legibility and simplicity.

Check out Adobe’s documentation to learn more about conditional statements.

Which do you prefer and why?

8 thoughts on “Conditional Statements: Switch Versus If/Else if/Else

  1. I really like one particular point about Switch statements that makes them well suited for some situations. You can leave break statements out to allow the code to execute through multiple Case statements until a final break is found. This can be handy under the right circumstance. For example, say you are using a numeric variable called _alertLevel in a Switch with 1 being the lowest level and 3 being the highest. Put the code in the Switch in reverse order where Case 3 is first. Case 3 would turn on a sound for a siren (no break statement), Case 2 would turn on an animation of a light flashing (also, no break statement), and Case 1 would display a message saying “Alert!” with a break statement at the end. When you go to alert level 1, only a message is displayed. Going to alert level 2 would turn on a light and display the message. Alert level 3 would turn on the siren sound, the light and display the message. Funny thing is, I’ve worked with career programmers who have used Switch statements for a decade or more and have never thought to use a Switch in this way.

  2. Well, I would definatelly use ‘switch’ any time when I have more than 2 conditions to a single variable.
    But, if + else if + else if + else is really the only way to go when condition should check 2, 3 or even more values.
    Also, switch is more readable.
    And. I prefer using ?: when performing simple actions upon condition check.

  3. Not really necessary to use switch because I can’t do if (x !=0 ) with it and it doesn’t allow me also to use multiple statements, so U can use switch if everything is equal, but when it comes for non-equality, U’ve stuck.

  4. Use switch when you want to easily enumerate the positive values the variable you are evaluating can take.

Comments are closed.