Do you prefer Pepsi or Coke? Of course, we all know the correct answer is Mt. Dew, but that is besides the point. When it comes to ActionScript the answer itself can be asked, and then responded to, using conditionals.
If your familiar with other programming languages you can probably glance over the next paragraph or two. If your new to the whole game then you’ll want to hang around. Conditionals are exactly what they sound like. They are testing a condition. When the test is complete we respond to it in a particular way.
For example, “Do you prefer Pepsi or Coke?”
“Coke”
“Congratulations, here is a glass of ice cold Coca Cola.”
We asked the question and then responded to it. This is the essence of a conditional statement. You will use them all the time in your programming so it’s a good idea to get a handle on them. There are two primary conditional statements in ActionScript. If statements and switch statements.
if
If statements are the most common form of a conditional. In simple form they are asking, if something is true then do the following. Here is an example using ActionScript 3 syntax:
var drink:String = "Coke"; //Declare our drink variable
if (drink == "Coke") {//Conditional Statement
trace("Here's a nice cold glass of Coke");
}
Let’s look at what we did here. The first thing we did was declare a variable and set it equal to the string “Coke.” Line 3 starts the conditional statement. The question we are asking resides inside the parentheses “()”. In this case we are asking if the variable drink is equal to the string “Coke.” Our response resides in the brackets “{}”. The answer to our condition is true, so we will trace out the string “Here’s a nice cold glass of Coke.”
Cool. But what if the person likes Pepsi instead of Coke? What happens then? If we use the code from below nothing would happen.
var drink:String = "Pepsi"; //Declare our drink variable
if (drink == "Coke") { //Conditional Statement
trace("Here's a nice cold glass of Coke");
}
Nothing happens because the statement evaluates to false and we haven’t provided an alternative. Bummer. This guys not getting his Pepsi. Let’s fix that. If has a close friend else. Else provides an ultimatum. That way if our statement evaluates to false like the code above we still perform some action. Let’s look at how we add that in our code.
var drink:String = "Pepsi"; //Declare our drink variable
if (drink == "Coke") {//Conditional statment
trace("Here's a nice cold glass of Coke");
} else {// Our ultimatum
trace("Sorry, we're out of Pepsi. Would you like Coke instead?");
}
Okay so you noticed at line 5 we added an “else” clause. This essentially is saying, if everything else fails in the condition do this. So if this man’s favorite drink is anything other than Coke, perform whatever actions are in the else statement. In this case Flash would trace, “Sorry, we’re out of Pepsi. Would you like a Coke instead?”
Hopefully this is all making sense up until this point. There is yet another step we can throw into our code for more specific results. It doesn’t do us much good to just have an either/or conditional. We may want to specify multiple conditions and evaluate which one occurs. This brings in the else if statement. You can repeat else if as many times as you like in your code for as many possibilities as you would like to test. Here is an example in code.
var drink:String = "Pepsi"; //Declare our drink variable
if (drink == "Coke") {//Conditional statment
trace("Here's a nice cold glass of Coke");
} else if(drink == "Pepsi") {// Conditional statment
trace("Here's a nice cold glass of Pepsi");
} else if(drink == "Mt. Dew") {// Conditional statment
trace("Here's a nice cold glass of Mt. Dew");
} else if(drink == "Sprite") {// Conditional statment
trace("Here's a nice cold glass of Sprite");
} else {//Our ultimatum
trace("Sorry dude, looks like your going be thirsty.");
}
Now we have multiple options to test. This allows us to get a little more specific in our code and get a very specialized outcome. You can start to imagine the potential of the conditional if statement.
Switch
Another common form of the conditional statement is the switch statement. Switch is often a replacement for long if, else if statements. They tend to look a bit cleaner and are easier to read. Ultimately, this is a preference thing. They can both accomplish the same end-goal, but just look different in doing so.
Here is an example switch statement that follows the form of our pop debate.
var drink:String = "Pepsi"; //Declare our drink variable
switch (drink) {
case "Coke" : //Checking to see if the case is equal to drink
trace("Here is some delicious Coke"); //If the case is true, this will trace out
break; // Exits the switch statement
case "Pepsi" :
trace("Here is some delicious Pepsi");
break;
case "Mt. Dew" :
trace("Here is some delicious Mt. Dew");
break;
default :
trace("Dude, you're having a bad day. We still don't have any liquid.");
break;
}
You can see that it’s a bit cleaner looking. How does it work? Well, in the parenthesis we place what we are trying to determine the truth of. In this case we are trying to determine what “drink” is equal to. To do that we specify a case. The cases here are Coke, Pepsi, and Mt. Dew. If, at any time, one of those cases becomes true the line of code underneath will execute. Lastly, we have chosen to include a break statement, which means that if this particular case evaluates to true, exit the switch.
The default statement on line 13 is optional and is exactly the same as the else statement. It is our ultimatum if everything else evaluates to false.
Logical Operators
At some point your going to want to know more than whether a condition is true or false. There are other comparison operators such as:
- (==) equals
- (>) greater than
- (>=) greater than or equal to
- (<) less than
- (<=) less than or equal to
You can also combine the above comparisons together to get ever greater specificity. The following are called logical operators:
- (&&) AND
- (||) OR
- (!) NOT
In simple language and example might be, “Do you like Pepsi andMt. Dew?” Logical operators really open up your opportunities to get specific in your code.
Well, that’s about it for conditional statements. Let me know if there are any questions. Now, I’m off to go drink a real Mt. Dew!

Good tut. Definitely something you’ll use in your coding