actionscript 3.0 for journalists

Conditional Statements

Conditional statements are the foundation to logical expression in virtually every programming language.

Oftentimes, you will want your program to make a decision based on a set of conditions. If a condition is true then you will want to perform some type of action, otherwise if that condition is false, then you may want your program to perform a different action.

In ActionScript, as with many languages, we do this with the if keyword. The word "if" is preceded by a set of parenthesis which contain the condition. If the condition is true, then Flash will execute the code in the curly brackets, otherwise it will ignore the statement altogether. Take the following example in which we test the variable "age" to see if the value is 21.

if(age == 21) {
      response_txt.text = "You are old enough to have a drink"; 
}

In this piece of code, we assess the condition (age == 21) to see if it is a true statement. The double-equals symbol is the equivalency operator, which is used to compare two values to see if they are equal. The code that is between the parentheses will only run if the condition is true, otherwise it skips over it completely.

This example has only limited practicality because it tests to see if the age variable is exactly equal to 21. Really, it should be testing if the age variable is over 21. We do this with the more than or less than operator.

if(age > 20) {
       reponse_txt.text = "You are OVER 21, you can have a drink";
 }

Notice the difference from the previous example. We are now asking the question if the age variable is more than 20 (We check to see if it is over 20 instead of 21 because the more than does not include the value itself.)

Checking Multiple Conditions

Suppose we wanted to test if two or more conditions were true. We have a few options available to us. We need to know if we want both conditions have to be true, or if either condition could be true. This is called Boolean logic and uses the terms AND and OR to test values.

Let's pretend we are writing an application that will test to see if a child is old enough and tall enough to go on an amusement park ride:

if(age > 12 && height > 48){
      response_txt.text = "You can ride the Ferris wheel"; 
}

Notice the double-ampersand which is the AND operator. In this situation BOTH conditions have to be met in order for Flash to execute the code in-between the curly brackets. But what if the requirements said that either of these conditions could be met?

if(age > 12 || height > 48){
      response_txt.text = "You can ride the Ferris wheel"; 
}

Notice the double-bar symbol. This is the OR operator. Either condition could be met for Flash to execute the code within the curly brackets. The only way Flash will ignore the statement is if both conditions are false.

List of available operators

Here is a table of operators that you can use in conditional statements.

Conditional Operators
Symbol Description
== The EQUIVALENCY operator requires both sides to be exactly the same for the statement to be true.
&& The AND operator requires several conditions be met in order for a statement to be true.
|| The OR operator requires one of several conditions to be met in order for the statement to be true.
> The MORE THAN operator requires the left side to be greater than the right side for the statement to be true.
< The LESS THAN operator requires the left side to be less than the right side for the statement to be true.
>= The MORE THAN OR EQUALS operator requires the left side be more than or the same as the right side for the statement to be true.
<= The LESS THAN OR EQUALS operator requires the left side be less than or the same as the right side for the statement to be true.

Here are a few examples of these operators in action:

In this example, three conditions have to be met for the response text to be shown; 1) you have to be 21 or over, 2) you have to have an ID card and 3) You have to pay the $10 cover charge

if(age >= 21 && idCard == true && cover == 10){
        response_txt.text = "Welcome to the club"; 
}

In this example, a photo slideshow counter in Flash has to be less than 10 or have a null value:

if (currentImage < 10 || currentImage == null){
         gotoAndStop("startSlideshow"); 
}

Using the Else keyword

Let's say the condition from any one of the examples above is not met. Flash will ignore the block code in the curly brackets and continue executing the program normally. However, in many cases you may want to execute a different set of code, but only if the condition is not met. This is where the else keyword comes into play. The else allows you to post a different block of code that will execute should the condition not be met.

if(gameScore > 1000){
        response_txt.text = "Congratulations, you passed 1,000 beating the game!";
 } else { 
       response_txt.text = "Sorry, you didn't reach score of 1,000 yet, try again.";
 }

In this example, we use the else keyword to present a second set of brackets that presents an alternative block of code to execute should the condition be false. It is important to note the difference of using the else keyword and simply running the code without it. Flash will only run the else block of code if the condition is false. Take these two examples:

Example 1: Using else keyword

var score:Number = 1001;  
if(score > 1000){ 
        trace("You win");
 } else { 
       trace("You lose"); 
}  

output:  you win

Example 2: Excluding the else keyword

var score:Number = 1001;  
if(score > 1000){  
       trace("You win"); 
} 
trace("You lose");   

output:  you win you lose 

Without the else keyword, Flash will execute the code following the "if" statement. But if you include the else keyword, you create an either/or situation in which Flash only executes the code should the condition be false.