If Statement

If 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, Else Statement, If, Else If Statement, If, Else If, Else Statement, or Switch Statement.

In this example we will demonstrate the If 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=2;

var value02:uint=3;

var value03:uint=4;

var value04:uint=4;

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

trace("This is a true statement")


Step Four

Test your movie by pressing Cmd/Return (PC - Ctrl/Enter). This is a True 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 2 less than 3 (yes) and is 4 equal to 4 (yes)? Since the statement is true, the code between the curly brackets is run and This is a true statement appears in the Output panel.

If you were to change the values to make the statement false, nothing would appear in the Output panel.


Stock Images