< Day Day Up >
Loop Exceptions
In general, a loop continues to perform iterations until its condition is no longer true. You
use two actions to change this behavior: continue and break.
With the continue action, you can stop the current iteration (that is, no further actions in
that iteration will be executed) and jump straight to the next iteration in a loop. For
example:
var total:Number = 0;
var i:Number = 0;
while (++i <= 20) {
if (i == 10) {
continue;
}
total += i;
}
The while statement in this script loops from 1 to 20, with each iteration adding the
current value of i to a variable named total—until i equals 10. At that point, the continue
action is invoked, which means that no more actions are executed on that iteration and the
loop skips to the eleventh iteration. This would create the following set of numbers:
1 2 3 4 5 6 7 8 9 11 12 13 14 15 16 17 18 19 20
N
otice that there is no number 10, indicating that no action occurred on the tenth loop.
The break action is used to exit a loop, even if the condition that keeps the loop working
remains true. For example:
var total:Number = 0;
var i:Number = 0;
while (++i <= 20) {
total += i;
if (total >= 10) {
break;
}
}
This script increases the value of a variable named total by 1 with each iteration. When
the value of total is 10 or greater—as checked by an if statement—a break action occurs
and the while statement halts, even though it's set to loop 20 times.
In the following exercise, you'll use continue and break in a simple search routine.
1. Open phoneNumberSearch1.fla in the Lesson09/Assets folder.
This file contains two layers: Actions and Search Assets. The Actions layer will
contain the search routine for this project. The Search Assets layer contains the
text fields, button, and graphics for this exercise.
In this exercise, you'll produce a simple application that lets you enter a name in a
search field to return the phone number for that individual. Two text fields are on
the screen: name_txt will be used to enter the name to search; result_txt will
display the search result. An invisible button over the Search button graphic has an
instance name of search_btn that will be used to call a search function.
2. With the Actions panel open, select the first frame in the Actions layer and add the
following script:
3.
4. var info:Array = [["John","919-555-5698"],["Kelly","232-555-3333"],
["Ross","434-555-5655"]];
5.
This script creates a two-dimensional array called info containing three elements,
each of which is its own array, or sub-array. The first element of each sub-array is
a name, and the second element of each sub-array is a phone number.
To access the first name in this array, you use info[0][0]; the first phone number
would be accessed using info[0][1]. This syntax represents John's name and
number. The syntax will play an important role in the search function we're about
to script.
3. Add this function definition after the info array:
4.
5. function search () {
6.
7. var matchFound:Boolean = false;
8.
9. var i:Number = -1;
10.
11. while (++i < info.length) {
12.
13. }
14.
15. }
16.
You've begun to define the function that will search the info array for a specific
phone number. The first action in this function creates a variable called
matchFound and assigns it an initial value of false. (We'll soon show you how this
variable will be used.)
We've set up the while statement to loop once for every element in the info array.
4. Add this action to the while loop in the search() function:
5.
6. if (info[i][0].toLowerCase() != name_txt.text.toLowerCase()) {
7.
8. continue;
9.
10. }
11.
12. result_txt.text = info[i][1];
13.
14. matchFound = true;
15.
16. break;
17.
With each iteration of the loop, the if statement uses the current value of i to
determine whether mismatches exist between names (made all lowercase) in the
info array and the user-entered name (also forced to lowercase) in the name_txt
text field.
When a mismatch is encountered, the continue action within the if statement is
evoked and the script skips to the next loop. Using toLowerCase() to convert
names to lowercase makes the search case insensitive.
If a name in the info array matches one in the name_txt text field, continue is not
invoked and the actions after the if statement are executed. Using the value of i at
the time a match was found, the first action sets the value of the variable
result_txt.text to the matching phone number, sets matchFound to true, and
executes the break action to halt the loop.
To understand better how this action works, imagine that someone has entered
Kelly into the name_txt text field. The location of Kelly in the info array is as
follows:
info[1][0]
On the first iteration of the loop, the value of i is 0, which means that the if
statement in the loop would look like this: