Table of Contents

8.6 Stopping a Loop Prematurely

In a simple loop, the test expression is the sole factor that determines when the loop stops. When the test expression of a simple loop yields false, the loop terminates. However, as loops become more complex, we may need to terminate a running loop arbitrarily, regardless of the value of the test expression using the break statement. A related statement, continue, ends the current iteration of the loop body and resumes execution with the next iteration.

8.6.1 The break Statement

The break statement ends execution of the current loop or switch statement (as we saw in Chapter 7). It has the modest syntax:

break

The only requirement is that break must appear within the body of a loop or switch statement.

The break statement provides a way to halt a process that is no longer worth completing. For example, we can use a for-in loop to build a form-checking routine that cycles through the input-text variables on a timeline. If a blank input field is found, we alert the user that she hasn't filled in the form properly. We can abort the process by executing a break statement. Example 8-3 shows the code. Note that the example assumes the existence of a movie clip called form_mc that contains a series of declared input variables named input01_txt, input02_txt, and so on.

Example 8-3. A simple form-field validator
for (var prop in form_mc) {
  // If this property is one of our "input" text fields
  if (prop.indexOf("input") != -1) {
    // If the form entry is blank, abort the operation
    if (form[prop].text =  = "") {
      displayMessage = "Please complete the entire form.";
      break;
    }
    // Any substatements following the break command are not reached
    // when the break is executed
  }
}
// Execution resumes here after the loop terminates whether
// due to the break command or the test condition becoming false

8.6.2 The continue Statement

The continue statement is similar to the break statement in that it causes the current iteration of a loop to be aborted; but, unlike break, it resumes the loop's execution with the next natural cycle. The syntax of the continue statement is simply:

continue

In all types of loops, the continue statement interrupts the current iteration of the loop body, but the resumption of the loop varies slightly depending on the type of loop statement. In a while loop and a do-while loop, the test expression is checked before the loop resumes. But in a for loop, the loop update is performed before the test expression is checked. And in a for-in loop, the next iteration begins with the next property of the object (or element of the array), if one exists.

Using the continue statement, we can make the execution of the body of a loop optional under specified circumstances. For example, here we move all the movie clip instances that aren't transparent to the left edge of the Stage, and we skip the loop body for transparent instances:

for (var prop in _root) {
  if (typeof _root[prop] =  = "movieclip") {
    if (_root[prop]._alpha < 100) {
      continue;
    }
    _root[prop]._x = 0;
  }
}

8.6.3 Maximum Number of Iterations

As noted earlier in this chapter, loops are not allowed to execute forever in ActionScript. In Flash Player 5 or later, loops are limited to 15 seconds. The number of iterations that can be achieved in that time depends on what's inside the loop and the computer's speed. To be safe, you shouldn't create loops requiring more than even a few seconds to execute (which is eons in processing terms!). Most loops should take only milliseconds to finish. If a loop takes longer to complete (for example, because it's processing hundreds of strings while initializing a word-scramble game), it's worth rewriting the code using a timeline loop, as described in Section 8.7. Timeline loops allow us to update the progress of a script's execution on screen and avoid the potential display of the error message shown in Figure 8-1.

Figure 8-1. Bad loop! Down boy!
figs/act2.0801.gif

When a loop has run for more than 15 seconds in Flash Player 5 or later, an alert box warns the user that a script in the movie is delaying the movie's playback. The user is offered the choice to either wait for the script to finish or to quit the script.

Flash Player 4 is even stricter�it allows only 200,000 iterations, after which all scripts are disabled without any warning.

Take special heed: the 15-second warning that users see does not mention that canceling a runaway script will actually cause all scripts in the movie to stop functioning! If a user selects Yes to stop a loop from continuing, all scripts in the movie are disabled.


Table of Contents