ABSTRACT

Creating sections of code that repeat Flash has several options for repeating; the first option is used when you already know how many times you want to repeat a section of code. In this circumstance you use the ‘for’ statement:

sum = 0; for(i=1; i<=100; i++){

//Code that repeats 100 times with i=1, 2, 3.. 100 sum += i;

}

The ‘for’ statement takes three lines of code as a parameter. Because they are lines of code, they are separated by semi-colons not commas. The first line is the starting condition. Most ‘for’ loops use a counting variable, in this instance i. The first line is usually where you will set the start value for the variable that you use as a counter. The second line dictates the condition that will cause the ‘for’ loop to be executed, and as soon as this condition fails the ‘for’ loop is exited. In the example above, the exit condition occurs when i is no longer less than 100. The final code line in the ‘for’ statement parameter is the repeat action, and this code snippet is executed each time the ‘for’ loop repeats. In the example above the English language equivalent would be ‘Set i = 0. Start the loop. If i is less than 100 then execute the code within the curly brackets. Having executed the code, increment (add one to) i. Now repeat from start of the loop.’