home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Book HomeActionScript: The Definitive GuideSearch this book

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 first block of code to be executed. If the condition is not met, an alternate block of code may be executed (and that alternate block may, in turn, have its own condition). Essentially, we set up flowchart-like logic in our program that, in pseudocode, reads like this:

if (the first condition is met) {
  // Execute this code
} else if (the second condition is met) {
  // Execute this code
} ...otherwise {
  // Execute this code

Of course, we must describe each condition in terms the interpreter understands. Not a problem -- that's just a question of syntax, which we'll learn next. Conceptually, 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.

7.1. The if Statement

The if statement is your everyday, all-purpose conditional. We use if 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) {

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 = _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 non-numeric 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 learning about 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, "Operators":

lastName = "";
firstName = "";
if (firstName != "" || lastName != "") {  
  trace("Welcome " + firstName + " " + lastName);

Finally, we test whether a movie clip object exists:

if (myClip) {
  myClip._x = 0;  // If myClip exists, put it on
                  // the left edge of the Stage 

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.