jump to navigation

AS3 Loops: While vs. For vs. For Each September 22, 2010

Posted by Tournas Dimitrios in Actionscript.

It’s all about loops this time. Boring basic stuff, i know, but sometimes, if i haven’t coded AS3 for some weeks, my memory fails me. So here comes a short overview about the various looping techniques in Actionscript 3.0. In this article we will see what are the loops, the use of them, the different types of loop and how to use them.

The loops are blocks of code that are repeatedly performed up to when a certain condition (called condition of loop) has a true value, meaning the condition is right.
Actionscript 3.0 has 5 types of loop available .

  • for
  • while
  • do … while
  • for  … each  in
  • for  … in

The for loop :

This one is probably the most common loop statement. Basically, you have a variable that automatically changes and performs a certain action every time it changes. Here’s the syntax:

for (starting position; condition; action){
what to do}
for (var i:int=0; i<10; i++){
trace("Test" + i);

This is a really simple piece of code, which just traces 10 lines of text in the output window, such as Test0, Test1, etc, up to Test9. Why Test9 and not Test10? Because once i is 10, it isn’t smaller than 10, and that’s exactly what our condition requires.

We can use this loop to dynamically create objects, manage arrays, do anything that needs to be repeated to be done. It usually saves time too.

The while loop :

The while loops is pretty similar to the for loop.Let’s try getting the same Test0-9 results, but using this type of loop. Let me show you:

while (condition){
what to do


var i:int=0

The do …. while loop :

The do while loops is just like the while loop, with one exception – the action is done once before checking the condition, that means your action will happen at least once.

do {
what to do

var i:int=0
do {

The for … each in loop :

All the previous loop statements are used to perform an action repeatedly. This and the next loop are used for other purpose – you ‘feed’ the loop an object, and it tells you the values of all its properties.

For example, let’s create an object and give it some properties, then retrieve the values using the for each in loop.

var customer:Object = new Object();
customer.fullname = "Thomas Edison" ;
customer.age = 44 ;
customer.telephone = "23454321";

for each(var value:String in customer){
This code returns :
Thomas Edison

The code returns all the properties of customer object. The prp variable (doesn’t have to be called prp) returns the value for each property.

The for … in loop :

Almost the same as the previous loop. The previous loop returns the values of properties, while this one returns the names of the properies.

var customer:Object = new Object();
customer.fullname = "Thomas Edison" ;
customer.age = 46 ;
customer.telephone = "23454321" ;

for (var prp in customer){

This code returns:
//Tracing all the information

var num:Number = 1 ;
for (var value:String in customer){

trace("variable " + num + " :" + value +" ---->>" + customer[value]);
num ++;
//Returns the following 
//variable 1 :fullname ---->>Thomas Edison
//variable 2 :age ---->>44
//variable 3 :telephone ---->>23454321

There you go. Hope this helped!


1. Iteration and Recursion in PHP « Tournas Dimitrios - October 9, 2010

[…] The same consept also applays to actionscript read my article here >>>> […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s