
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
The else Statement
|
157
}
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 expres-
sion 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 con-
dition is not met and the first trace() statement is not executed. Similarly, the inter-
preter 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
}
The else Statement ...