
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
156
|
Chapter 7: Conditionals
Of course, we must describe each condition in terms the interpreter understands.
This isn’t a problem—it’s just a question of syntax, which we’ll cover next. Concep-
tually, all conditionals either allow or suppress the execution of a code block, based
on the specified condition. Now let’s see how conditionals work in practice.
The if Statement
The if statement is your everyday, all-purpose conditional. We can use an if state-
ment to create a two-pronged branch in our code, like a fork in the road. The if state-
ment 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 expres-
sion). If it is
true, the ...