The if statement is your everyday, all-purpose conditional. We can use an if statement to create a two-pronged branch in our code, like a fork in the road. The if statement contains one or more substatements that are executed only when the specified condition is met. The if statement has the following syntax:
if (condition) { substatements }
An if statement starts, not surprisingly, with the keyword if. The condition that must be satisfied for substatements to be executed is enclosed in parentheses. The substatements are one or more ActionScript statements. Each substatement should be on its own line and terminated with a semicolon. The entire if statement ends with a closing curly brace (}), without a trailing semicolon.
The condition of our if statement can be any valid expression. When an if statement is executed, the interpreter checks the value of that expression (called the test expression). If it is true, the substatements are executed. Otherwise, the substatements are not executed. Here we use simple Boolean values as the test expression:
if (true) { trace("The condition was met!"); // This statement will be executed } if (false) { trace("The condition was met!"); // This statement is never executed }
Of course, there's no practical reason to use Boolean literals as test expressions, because their values never change. Instead, we'll use complex expressions that return Boolean values. For example, expressions that involve a comparison operation return a Boolean value, which makes them perfect for conditional test expressions:
var pointerX = _root._xmouse; // Horizontal location of the mouse // If pointerX > 300 yields true... if (pointerX > 300) { // ...this statement is executed trace("The mouse is past the 300 pixel mark"); }
Now for the cool part: the test expression of a conditional doesn't necessarily have to evaluate to a Boolean�any expression will do. We can use a string or a number as the test expression of a conditional:
if ("hi") { trace("The condition was met!"); } if (4) { trace("The condition was met!"); }
How does this work if the expressions "hi" and 4 are not Booleans? The answer lies in the marvels of datatype conversion, as shown in Table 3-3. When the test expression of a conditional statement is not a Boolean value, the interpreter converts the expression to a Boolean. For example, the interpreter converts "hi" to false because all nonnumeric strings convert to false when used in a Boolean context. So the condition is not met and the first trace( ) statement is not executed. Similarly, the interpreter converts the number 4 to true (any nonzero number converts to true), so the second trace( ) statement is executed.
All our earlier work with datatype conversion has paid off! Here are some basic applied examples. Try to guess whether each substatement will be executed:
x = 3; if (x) { trace("x is not zero"); }
This example uses the OR operator, described in Chapter 5:
lastName = ""; firstName = ""; if (firstName != "" || lastName != "") { trace("Welcome " + firstName + " " + lastName); }
Finally, we test whether a movie clip object exists (movie clips are converted to true when used in a Boolean context):
if (theClip_mc) { theClip_mc._x = 0; // If theClip_mc exists, put it on // the left edge of the Stage }