Chapter 12. Looping over Code

Write a For Loop

Frequently, you will need to perform repetitive tasks in your scripts. Like other programming languages, ActionScript provides for several different methods of looping over a block of code.

One of the most common types of loops is a for loop. To initialize the loop, you need to provide three key elements: the counter variable for the loop, the condition on which the loop will be tested, and a statement to change the value of the counter with each iteration of the loop. Following the initialization line, you will place the code to be executed with each iteration within a set of curly braces. For example, your loop might look like this:

for(var i:Number=0; i<10; i++)
{
     trace(i);
}

In this example, a variable, i, is created and set to an initial value of 0. After each iteration of the loop, the current value of i will be tested to see if it is less than 10: If it is, the loop will execute again; if not, it will exit. Finally, the value of i is incremented by 1 after each iteration. The code in this example simply outputs the value of i, so if this code were run, the Output panel would display the digits 0 through 9.

Write a For Loop

  • Write a For Loop
  • Write a For Loop
    Write a For Loop
  • Write a For Loop
  • Write a For Loop
  • Write a For Loop
  • Write a For Loop
  • Write a For Loop
  • Write a For Loop
  • Write a For Loop
  • Write a For Loop
  • Write a For Loop
  • Write a For Loop
    Write a For Loop
  • Write a For Loop

    The movie plays.

  • Write a For Loop

Apply It

You will likely encounter situations in which you need to programmatically stop a loop from processing. A common example is a loop that contains a conditional statement. For example, if you were looping over an array and attempting to find a particular value, you would want to stop the loop from continuing to process the rest of the array as soon as the value that you want is found. To do otherwise is at best a waste of resources and at worse might cause unexpected results if the value was discovered more than once in the array.

You can terminate a loop programmatically with the break statement. ActionScript will stop the loop immediately when the statement is encountered and pass the processing to whatever statement immediately follows the loop's closing curly brace.

Most often, the break statement will be contained within an if statement or some other conditional clause so that the script will encounter it only if a certain condition is met, such as a particular value being found.

Write a While Loop

You will use a for loop any time that you need to execute the same block of code a certain number of times. In the example presented in the section "Write a For Loop," exactly five text fields were created, so a for loop made sense. However, there will be times when, instead of looping a predetermined number of times, you will instead want to perform a set of instructions while a condition is true.

ActionScript provides a second looping construct for this scenario — the while loop. Whereas a for loop contains the creation of the variable, the test, and the increment within the initialization statement, a while loop contains only the test. The variable against which you will be testing will be created elsewhere in the code — somewhere above the while loop — and the incrementing of the variable will occur within the code to be executed by the loop.

In many situations, a for loop and a while loop can be written to achieve the same results, and for the most part, it will not matter which you use. A general rule is that if you have to loop over a variable that already exists in your code, you should use a while loop; if you are looping over a variable that will exist only for the purpose of the loop, then a for loop will usually be more appropriate.

In the example shown here, a while loop is used to step over a string containing a comma-separated list of names and counts the number of commas. After the loop, the number of names, which is known to be one more than the number of commas, is output. The loop relies on an if statement, which is discussed in detail in Chapter 11.

Write a While Loop

COUNT THE NUMBER OF NAMES IN A LIST

  • COUNT THE NUMBER OF NAMES IN A LIST
  • COUNT THE NUMBER OF NAMES IN A LIST
  • COUNT THE NUMBER OF NAMES IN A LIST
  • COUNT THE NUMBER OF NAMES IN A LIST
  • COUNT THE NUMBER OF NAMES IN A LIST
  • COUNT THE NUMBER OF NAMES IN A LIST
  • COUNT THE NUMBER OF NAMES IN A LIST
  • COUNT THE NUMBER OF NAMES IN A LIST
    COUNT THE NUMBER OF NAMES IN A LIST
  • COUNT THE NUMBER OF NAMES IN A LIST
  • COUNT THE NUMBER OF NAMES IN A LIST
  • COUNT THE NUMBER OF NAMES IN A LIST
  • COUNT THE NUMBER OF NAMES IN A LIST

TEST THE MOVIE

  • TEST THE MOVIE

    The movie plays.

TEST THE MOVIE
  • TEST THE MOVIE

Apply It

A third type of loop structure exists: the do while loop, which is similar conceptually to a while loop, but it has one major difference: The condition is at the bottom of the code, so the loop will execute before the condition is evaluated. This means that the loop's code is guaranteed to execute at least once, even if the condition is initially false — for example:

var x:Number = 5;
while(x < 5)
{
                   trace(x);
}

Executing this code would result in nothing being displayed in the Output panel, as the value of x is not less than 5. However, if you write this as a do while loop, using the same variable:

do
{
                   trace(x);
} while (x<5)

This code would cause 5 to appear, as the code within the loop — the trace statement — executes once before the conditional is tested.

Loop over an Array

An array is a special type of variable that enables you to store multiple values in one variable name. For example, if you have multiple characters in a story, you can create an array to store them together:

var characters:Array = ["Mal", "Zoe", "Wash", "Jayne", "Kaylee", "Inara", "Simon", "River", "Book"];

Individual elements of the array can be returned by referencing the array name and, in square brackets, the element that you want to view. Keep in mind that the first element of the array is 0. Therefore, in the preceding array, characters[5] would return Inara.

To access all elements of the array, you can use a loop. You could use a for loop, setting the test to one less than the length of the array, which can be determined with array.length. Or, if you were removing the elements as you accessed them, you could use a while loop, continuing until there were no elements left. Although both of these alternatives work, ActionScript actually provides a loop structure specifically designed for arrays, known as a for each . . in loop.

The basic initialization of the loop is simple: for each (CounterVariable in Array). CounterVariable is some arbitrary variable that will represent each returned element, and Array is the name of the array over which you are looping. As with other loops, you will follow the initialization statement with a set of curly braces, and within the braces, you will have the code that you want to execute, which will usually involve using a counter to view or manipulate the elements.

Loop over an Array

Place The Values of an Array in Text Fields

  • Place The Values of an Array in Text Fields
  • Place The Values of an Array in Text Fields
  • Place The Values of an Array in Text Fields
    Place The Values of an Array in Text Fields
  • Place The Values of an Array in Text Fields
  • Place The Values of an Array in Text Fields
  • Place The Values of an Array in Text Fields
  • Place The Values of an Array in Text Fields
  • Place The Values of an Array in Text Fields
  • Place The Values of an Array in Text Fields
  • Place The Values of an Array in Text Fields
  • Place The Values of an Array in Text Fields
  • Place The Values of an Array in Text Fields

Test the Movie

  • Test the Movie

    The movie plays.

  • Test the Movie
Test the Movie

Extra

You can also use the for each . . in loop to loop over the elements in an XML file. Using XML and looping over its values are covered in Chapter 17. The examples shown in this section are using the shorthand method of creating an array. For more information on arrays and alternative syntaxes for creating them, see Chapter 2.

Note that in a for each . . in loop, the counter variable automatically contains the value from each element of the array, so if you are looping over array characters with a counter i, you simply reference each element directly using i. This is in contrast to using a for loop with the array, in which you would need to reference characters[i] as the counter in the loop will not be a reference to the actual element but will instead simply be a digit.

In the initialization statement for the for each . . in loop, do not declare the counter variable as being of a particular data type. Because the values in an array can be of any type, you cannot predict what type the value being returned will be with each iteration.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.28.50