February 2, 2008

AS 3.0 Condition & Loops

Condition Statements

The if statement in ActionScript is the same as it is in many programming languages:

if (myValue == 1) {
doSomething();
}

The == comparison checks for general equality. You can also use >, <, >=, and <= respectively.

You can add else and else if to extend the if structure:

if (myValue == 1) {
doSomething();
} else if (myValue == 2) {
doSomethingElse();
} else {
doNothing();
}

You can also have more complex conditions with && and || . These represent the and and or comparison operators.

NOTE
You can no longer use keywords "and" and "or" as alternates to && and ||.

Loops

Loops is done with the for statement or the while statement. For statement has three parts:
initial statement, a condition, and a change statement.

for(var i:int=0;i<10;i++)>
doSomething();
}

Translation: The following code sets the variable i to zero, loops as long as it is less than ten, and increases the value of i each time through the loop.



No comments: