If, Else If, Else Statement

If, Else If, Else Statement

Flash CS4 ActionScript 3.0 Tutorial

 

How to Compare Values and Make Logical Boolean Decisions with Flash 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 Statement, If, Else If, Statement or Switch Statement.

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

var value02:uint=3;

var value03:uint=4;

var value04:uint=4;

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

trace("Statement #1 is true")

} else if (value01 == value02) {

trace("Statement #2 is true")

} else {

trace("Both statements are false")

};

Step Four

Click the Auto Format icon in the Actions panel.

Step Five

Test your movie by pressing Cmd/Return (PC - Ctrl/Enter). Both statements are false 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 4 less than 3 (no) and is 4 equal to 4 (yes)? Since the statement is false, we move on to the next question. The else if questions if value01 equals value02 (no). Since both statements are false, the else makes Both statements are false appear in the Output panel.

If you were to set the value01 variable to 2, it would make the the first statement true and Statement #1 is true would appear in the Output panel.

Changing value01 variable to 3 would make the 2nd statement true.

 

Shutterfly 50 Free Prints 160x600