Home » ActionScript 2.0, ActionScript 3.0

ActionScript Basics part 6

24 November 2010 One Comment
Share on Facebook

In this series of learning Flash ActionScript Basics, which can be used in both ActionScript 3.0 and ActionScript 2.0.

Before going through this ActionScript article, go through the six parts of the starters to get your self convenient with Actionscript Basics
Learn about ActionScript Statements: ActionScript basics part1

Learn about Comments in AS: ActionScript basics part2

Learn about Variables: ActionScript basics part3

Learn about basics of Arrays: ActionScript basics part4

Learn about how to use conditional statements: ActionScript basics part5

Learn about Looping statements: ActionScript basics part6

Loops are used to execute same statement of code for definite number of times.

There are three kinds of loops in ActionScript, the for loop, the while loop, and the do loop. There are also two other kinds that are loops used specifically for

traversing a collection such as an Array, and can’t be used in any other way. These are the for…in loop and the for…each loop.

For Loop

The most frequently used looping statement ain any language is For loop.

for (var i:int = 0; i < 10; i++) {

The for loop contains three expressions

1. Initialization
2. Condition
3. Increment/decrement

The initializer is really just a statement that runs as the very first thing in the loop. It gets run once, and it gets run first. So in this example the first thing we do is create a variable called “i” and set it to 0.

The condition is an expression that should evaluate to true or false. Think of it as something that would be at home as the condition of an if statement. The for loop uses this condition before every iteration of the loop to see if it should continue executing. As long as the condition is true, the loop will continue to execute. As soon as it’s false, it will break out of the loop. In our example, this means that as long as the variable i has a value that is less than 10, we will continue the loop. Should it ever equal 10 or be greater than 10, the loop will stop.

Finally, the increment is another statement that will execute once for every iteration of the loop, after all of the code between the curly braces has executed. Looking at our example, this is the final piece of the puzzle, and we can see that after we trace “Hello,” we take that i variable and increment it by 1.

While Loop

The while loop will run until a condition is met, or as long as the condition is true. While loops are similar to for loops in how you can run something multiple times using one statement, but while loops are better for running a statement until an event occurs.

var i = 6;

while (i <= 6) {

    trace("This is the" + i + "statement\n");


In the above example, the variable is set to a value of 6, but that does not mean the loop will run 6 times. The condition says that while the value of i is less than or equal to 6, write the specified statement to the HTML document. In this case the while loop while only run once.


Used to enumerate the dynamic properties of an Object or Array.
Using for in we cannot enumerate the fixed properties of class.

The order of the elements may vary from the order in Object.

var obj:Object = {country:"India",states:28,unionTerritories:7};

for( var str in obj)

  trace(str +" : "+obj[str]);


For each..in

Used not only to iterate the XML Objects but also to iterate Object and Array.

We can iterate only through the dynamic properties of the Class again. To iterate through the properties of
user defined class we have to declare the properties of the method using the Dynamic keyword.

var obj:Object = {country:"India",currency:"Indian Rupee", capital:"New Delhi"};

for each( var str in obj)



The differnce between the for..in and for each..in loop is

for..in loop iterates through the property names and for each..in loops through the values.

No related posts.


We don't share your email anywhere, grab our rss via feedburner

  • camnpr

    Very concise as tutorial, I like it very much.