About statements 143
For more information on conditions that you can use, and how to write them, see the
following topics:
■ “About writing conditions” on page 143
■ “Using the if statement” on page 144
■ “Using the if..else statement” on page 145
■ “Using the if..else if statement” on page 146
■ “Using a switch statement” on page 147
■ “Using try..catch and try..catch..finally statements” on page 149
■ “About the conditional operator and alternative syntax” on page 152
About writing conditions
Statements that check whether a condition is true or false begin with the term if. If the
condition evaluates to
true, ActionScript executes the next statement. If the condition
evaluates to
false, ActionScript skips to the next statement outside the block of code.
The following statements test three conditions. The term
else if specifies alternative tests to
perform if previous conditions are false.
if ((passwordTxt.text.length == 0) || (emailTxt.text.length == 0)) {
gotoAndStop("invalidLogin");
} else if (passwordTxt.text == userID){
gotoAndPlay("startProgram");
}
In this code snippet, if the length of the passwordTxt or emailTxt text fields is 0 (for example,
the user hasn’t entered a value), the Flash document redirects to the
invalidLogin frame
label. If both the passwordTxt and emailTxt text fields contain values and the passwordTxt
text field’s contents match the
userID variable, the SWF file redirects to the startProgram
frame label.
If you want to check for one of several conditions, you can use the
switch statement rather
than multiple
else if statements. For more information on switch statements, see “Using a
switch statement” on page 147.
Refer to the following sections to learn how to write different kinds of conditions in your
ActionScript applications.
TIP
To optimize your code’s performance, check for the most likely conditions first.