Chapter 7. Conditionals
Most of
the code examples we’ve seen so far have been very
linear
—
each statement
is executed in turn, starting with the first and ending with the
last. Linear code always does the same thing. A
conditional, by contrast, is a type of statement
that performs an action only when a specified condition is met. In
linear code, the interpreter might execute statement A, then
statement B, then statement C. With conditional statements we can
tell the interpreter to execute statement A, then execute
either statement B or
statement C, depending on condition X.
We can use conditionals to create and control situations that have more than one potential outcome. For example, suppose we want to create a password-protected site. When users attempt to log in, either the password is correct and the user is allowed to enter the site, or the password is wrong and the user sees an error message. The two outcomes require two very different blocks of code in our movie’s script. One block needs to send the Flash playhead to a frame containing the site’s welcome screen, and the other block needs to send the playhead to a frame with an error message. But only one of the blocks should be executed when the user attempts to log in. A conditional statement allows us to execute the appropriate block and skip the inappropriate one.
How does the interpreter know which code block to execute? When we define a conditional statement, we specify the condition that must be met in order for the ...
Get ActionScript: The Definitive Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.