Prerequisite knowledge

You need to be familiar with ActionScript 3 variables, functions, objects, and conditionals.

User level


A loop is a control structure that repeats a block of code multiple times. The loop stops when a condition you have specified is met. Loops are one of the most commonly used structures in programming. You will find many when you examine the code for any application. This article focuses on three types of loops in ActionScript 3: for , while , and do…while .

for loops

for loops are used in many programming languages. If you are familiar with C, C++, C# or Java, the for loop syntax in ActionScript 3 will look similar. A for loop consists of two main parts: a header and a body. The header contains the initialization, the test, and the update of the loop. Each part of the header is separated by a semicolon. The body contains the code that will be repeated by the loop.


Each for loop has to be set up or initialized. The initialization statement runs once before looping begins. It declares and assigns the iteration variable. The iteration variable is the starting point for the counting the loop is going to do. It is commonly named i with a data type of int.

The iteration variable is like any other variable—you have to declare it before you can assign it a value. Don't forget to use the var keyword and assign it a data type. The data type of the iteration variable will depend upon the test and update of the for loop. int, uint, and Number are all valid types to use in a for loop.

If there is more than one for loop within a single function, the iteration variables will frequently be named i, j, k, and so on.

Examples of for loop initialization:

for(var i:int=0; ...){…} for(var i:int=9; ...){…} for(var j:int=1000000; ...){…} for(var k:uint=1026; ...){…} for(var l:Number=3.14; ...){…}


Every for loop needs an end point. A conditional expression is used to determine when a loop should stop running. That test is evaluated at the beginning of each iteration. When the test evaluates to false, the loop stops and is exited.

You use comparison operators to define your test:

for(…; i<10; …){…} for(…; i<=10; …){…} for(…; i>10; …){…} for(…; i>=10; …){…}

When you use a literal number in your test, as in the examples above, your loop will run a known number of times. You can also use a variable that could change under different conditions:

for(…; i<array.length; …){…} for(…; i<totalUsers; …){…}


When updating the iteration variable, for loops can count up using the increment operator ++, down using the decrement operator --, or by any number using the arithmetic compound assignment operators +=, -=, *=, /=, or %=.

Examples of for loop update statements:

for(…; …; i++){…} for(…; …; i--){…} for(…; …; i+=5){…} for(…; …; i-=7){…} for(…; …; i*=2){…} for(…; …; i/=10){…} for(…; …; i%=3){…}

Loop body

The code inside of the { } is considered the body of the loop. All of the code in the body of the loop will be executed during each iteration. Whatever code you would like to execute repeatedly is the code that you should put in the loop body.

for(…; …; …;){ trace(“Hello, world”); }

You can do calculations that vary, use the iteration variable to do something, call other functions, loop over an array, or instantiate objects—whatever code that needs to run over and over again. One thing to note: the code in the loop body should not change the value of the iteration variable. The loop's update statement in the header should be the only thing that changes the iteration variable.

Examples of for loops

Now that you understand the required parts of a for loop, look at some working examples of for loops and their output.

The for loop below simply traces the iteration variable. The loop will stop once i is no longer less than 10.

for(var i:int=0; i<10; i++){ trace(i); } //output 0 1 2 3 4 5 6 7 8 9

The for loop below also traces the iteration variable. This time the update statement decrements the iteration variable. The loop will stop once i is no longer greater than 10.

for(var i:int=20; i>10; i--){ trace(i); } //output 20 19 18 17 16 15 14 13 12 11

Once again, the for loop below traces the iteration variable. This time the update statement increments the iteration variable by 5. The loop will stop once i is no longer less than 20.

for(var i:int=0; i<20; i+=5){ trace(i); } //output 0 5 10 15

The for loop in the function below instantiates a new instance of a TextField (a core ActionScript 3 class) during each iteration. The iteration variable is used to set the x and y position of each instance. Because i changes each iteration, the x and y position will be different for each instance.

public function makeMessages():void{ for(var i:int=0; i<8; i++){ textMessage = new TextField(); textMessage.x = i * 10; textMessage.y = i * 30; textMessage.text = "hello"; addChild(textMessage); } } //values for x and y 0, 0 10, 30 20, 60 30, 90 40, 120 50, 150 60, 180 70, 210

Related to for loops are and for loops. Each of these loops are used to iterate through collections of data like Object, Array, and XMLList. Read more about them in the ActionScript 3.0 Reference at and for

while loops

You use a for loop when you know exactly how many times you want a loop to run. But what if you don't know how many times something should happen? What if you want something to happen as long as a condition is true rather than for a set number of times? You would use a while loop in those situations. That type of loop continues to run as long as the conditional expression is true.

Unlike a for loop, a while loop does not have an iteration variable or an update in its header. You only write a conditional expression in the header of a while loop. You must set up the body of a while loop very carefully. The code in the body must eventually cause the conditional expression to become false or your while loop will never stop running and you will have an infinite loop.

Look at the code below. A variable— monthInAYear —is declared and assigned a value. That variable is used in the conditional expression for the while loop. In the body of the while loop, monthInAYear is incremented. It will eventually become larger than 12, therefore stopping the loop from running.

public var monthInAYear:int = 0; while(monthInAYear < 12) { trace(monthInAYear); monthInAYear ++; } //output 0 1 2 3 4 5 6 7 8 9 10 11

The conditional expression is only re-evaluated at the beginning of each iteration, not during. In other words, the conditional expression is only re-evaluated after all of the code in the loop body finishes executing. Therefore, whatever code causes the conditional expression to be false should be the last thing in the loop body. If it is not the last thing, the other code will be executed although the condition has become false. Compare the code example above with the one below. Notice the difference switching the order of the two statements in the loop body makes. When monthInAYear is 11, the conditional expression evaluates to true so the loop body is entered. Once inside the loop body, monthInAYear is incremented to 12, then traced. It is not until after the trace statement that the conditional expression is re-evaluated.

public var monthInAYear:int = 0; while(monthInAYear < 12) { monthInAYear ++; trace(monthInAYear); } //output 1 2 3 4 5 6 7 8 9 10 11 12

do..while loops

The do..while loop is very similar to a while loop. You use it when you want the code in the body to run at least once, regardless of the initial state of the condition. A do..while loop works in the following way:

  1. The statement runs
  2. Whatever is being tested is updated
  3. The conditional expression is declared following while
  4. If the conditional expression is true, the cycle continues
public var monthInAYear:int = 12; do{ trace(monthInAYear); monthInAYear++; }while(monthInAYear < 12); //output 12

break and continue

Sometimes you need to change the flow of a loop. Maybe you were looping over an array and you found the data you were searching for. Or maybe something in the application has changed and the loop is no longer necessary. ActionScript 3 has break and continue to use in those situations.


The break statement stops the loop immediately. Code in the loop body following the break statement is not executed. break stops the loop even if the result of the conditional expression in your loop is true. The code below shows break being used to exit the loop. Althought the loop is set up to run until i is no longer less than 23, the break statement causes it to stop when i is 13.

for(var i:int=1; i<23; i+=4){ if(i==13){ break; } trace(i); } //output 1 5 9


The continue statement pauses the execution of the loop body, applies the loop's update, and then restarts the code in the loop body. The code below shows continue being used to skip an iteration of the loop. Notice that 13 is not in the output. When i is 13, continue causes the loop to pause, the update is applied making i 17, and then the loop continues.

for(var i:int=1; i<23; i+=4){ if(i==13){ continue; } trace(i); } //output 1 5 9 17 21

Using break and continue gives you more precise control over your loops.

Where to go from here

Whether you know exactly how many times a loop should run or under which conditions the loop should run, you can use loops to repeat a block of code multiple times. Understanding and using loops correctly allows you to perform operations that would be tedious or impossible if you had to hand-code each iteration. Loops are frequently used with objects, arrays, vectors, and XML data (coming soon).