User Guide
86 ActionScript Language and Syntax
if..else if
You can test for more than one condition using the if..else if conditional statement. For
example, the following code not only tests whether the value of
x exceeds 20, but also tests
whether the value of
x is negative:
if (x > 20)
{
trace("x is > 20");
}
else if (x < 0)
{
trace("x is negative");
}
If an if or else statement is followed by only one statement, the statement does not need to
be enclosed in braces. For example, the following code does not use braces.
if (x > 0)
trace("x is positiveā);
else if (x < 0)
trace("x is negative");
else
trace("x is 0");
However, Adobe recommends that you always use braces because unexpected behavior can
occur if statements are later added to a conditional statement that lacks braces. For example,
in the following code the value of
positiveNums increases by 1 whether or not the condition
evaluates to
true:
var x:int;
var positiveNums:int = 0;
if (x > 0)
trace("x is positive");
positiveNums++;
trace(positiveNums); // 1