If, Else Statement

If, Else Statement

Flash CS4 ActionScript 3.0 Tutorial


How to Compare Values and Make Logical Boolean Decisions with ActionScript



Conditional Statements are used to test equations to see if they are true or false. If the statement is true, one event happens. A false outcome would drive a different event.

There are multiple Conditional Statements in ActionScript. For more information, see topics, If Statement, If, Else If Statement, If, Else If, Else Statement, or Switch Statement.

In this example we will demonstrate the If, Else Statement.

Conditional Comparisons

Often times values are compared to determine if one is larger, they are equal, or not equal to each other. The following is a list of ActionScript comparisons. The And and Or symbols are used with more complex comparisons.

Equals (==)

Not Equal To (!=)

Greater Than (>)

Greater Than or Equal To (>=)

Less Than (<)

Less Than or Equal To (<=)

Or (||)

And (&&)


Step One

Open a new document and name the default layer, Actions.

Step Two

Option double-click (PC - Alt double click) the first keyframe of the Actions layer to open the ActionScript panel.

Step Three

Copy/Paste the following code in the ActionScript Panel:

var value01:uint=3;

var value02:uint=3;

var value03:uint=4;

var value04:uint=4;

if(value01 < value02 && value03 == value04) {

trace("This is a true statement")

} else {

trace("This is a false statement")


Step Four

Click the Auto Format icon in the Actions panel.

Step Five

Test your movie by pressing Cmd/Return (PC - Ctrl/Enter). This is a false statement should appear in the Output panel.

Dissecting the Code

The first four lines establish four new variables named value01, value02, value03, and value04. Each of the variables is set to a starting value.

The if statement asks, is 3 less than 3 (no) and is 4 equal to 4 (yes)? Since the statement is false, the code between the else curly brackets is run and This is a false statement appears in the Output panel.

If you were to set the value01 variable to 2, it would make the statement true and This is a true statement would appear in the Output panel.