AS3: The "If" Conditional

By Riyadh Al-Balushi | Flash CS3 | ActionScript 3.0 | Beginner

Conditionals are special tools that add logic to an ActionScript program by taking an action only upon the satisfaction of a specified condition. Conditionals are easy to understand and are guaranteed to appear in any ActionScript project you work on. This tutorial will teach how to use the most commonly used conditional: if.

The outline of this tutorial is as follows:

  1. Basic Usage of the if Conditional.
  2. Alternative Conditionals - else and else if.
  3. Comparison Operators.
  4. Logical Operators.

Basic Usage of the if Conditional

A conditional is an ActionScript tool used to take a decision upon the satisfaction of a specific condition. The most commonly use conditional is if. The basic task of this tool is simple: if something is true, only then it will take the action you specify. The if command can be broken down into two segments:

  1. The Condition.
  2. The Statement to be executed if the Condition is satisfied.

It is written in the following manner:

if (condition){
statements;
}

Note that the condition is written between two normal brackets () while the statements are written between two curly braces {}. The condition between the brackets must be satisfied (or evaluate to true) for the action to be executed, if the condition is not satisfied then nothing happens.

The example below uses a very simple conditional. Flash will output the message only if age is greater than 18. If age is not greater than 18 then nothing will happen.

var age:Number = 20;

if (age > 18){
trace("You can enter the website");
}
The greater than sign > is a comparison operator. We will talk more about comparison operators below.

It is possible to execute more than one statements upon the satisfaction of the condition by putting them below each other within the curly braces:

var age:Number = 20;

if (age > 18){
trace("You can enter the website");
gotoAndStop("Home");
}

This is the basic usage of the if conditional. It is possible to extend it by using an else and an else if statements. We will explore these next.

Alternative Conditionals

It is possible to use a conditional to execute an alternative statement when the conditional is not satisfied by using the else command. Here is an example:

var age:Number = 20;

if (age > 18){
trace("You can enter the website");
gotoAndStop("Home");
} else {
trace("Sorry, you are too young to visit this website");
}

It is also possible to chain alternative conditionals by using an else if command which checks for the satisfaction of a second condition if the first one is not satisfied:

var score:Number = 55;

if (score > 50){
trace("Congratulations, you passed!");
gotoAndStop("Home");
} else if (score > 40){
trace("Urgh! You were very close, I think you should try again!");
} else {
trace("Dude, you are such a failure, I think that you should quit!");
}
It is possible to chain an unlimited number of else if commands, but it is better to use a switch command for that sort of functionality. Please refer to the ActionScript reference to learn more about the switch command.

You have now learnt how to use the simple and more complex version of the if conditional. We will teach you next how to use Comparison Operators to make conditional decisions.

Comparison Operators

Comparison Operators are, just like what the name suggests, tools used to compare two values against each other. They are usually used with conditionals to make decisions. All comparison operators give out the value true or false depending on the operation and the operands. We are going to go through the most commonly used comparison operators:

  1. Equality operator - ==: This operator checks if two values are equal to each other. You must remember that this one is two equal signs and not one. It can be used to compare strings (words and letters) as well as numbers:
    var language:String = "English";

    if (language == "English"){
    trace("Hello!")'
    } else if (language == "Arabic"){
    trace("Marhaba!");
    } else {
    trace("Error - No supported language was specified.");
    }
  2. Greater than - >: This operator checks if the value on the left is greater than the value on the right.
    var age:Number = 20;

    if (age > 18){
    trace("You can play.");
    } else {
    trace ("Sorry, you are too young to play this game.");
    }
  3. Less than - <: This operator checks if the value of the left is less than the value on the right.
    var weight:Number = 100;

    if (weight < 120){
    trace("The weight of your bag is over the weight limit.");
    } else {
    trace("The weight of your bag is fine.");
    }
  4. Greater than or equal to - >=: This operator checks if the value to the left is greater than or equal to the value on the right.
  5. Less than or equal to - <=: This operator checks if the value to the left is less than or equal to the value on the right.

Logical Operators

It is possible to create more powerful conditionals by using Logical Operators. These are tools used to create conditionals with complex requirements such as having more than one condition, or alterative conditions, or NOT having a condition.

The first logical operator we will talk about is AND - &&. This operator lets you execute the code only if all the conditions linked by && are satisfied. The example below will trace the text "You Won!" only if time equal zero AND the score is more than 100:

if (time==0 && score>100){
trace ("You Won!");
}

The OR - || operator on the other hand lets you supply alternatives to your conditional, so that the code will be executed if any of the supplied conditionals is satisfied. In the example below you will only see the message "You Lost!" if the number of lives equals zero OR if the time equals zero:

if (time==0 || lives==0){
trace ("You Lost!");
}

The last logical operator is the NOT - !, which executed the command if the condition is not true. For example, the code below will be executed if the value for sex does NOT equal "Female":

var sex:String = "Male";

if (sex != "Female"){
trace("Sorry, this section is for women only!");
}

Conditionals are pretty easy to use as you have seen. They add logic to your Flash movies and help you make smarter movies.

This concludes our tutorial, I hope that you have learnt something new from it. Feel free to post any questions you have at the Flash Forum.

- End of tutorial.