For loops can be useful for repeating an action multiple times.
In ActionScript we can use the following repetitive for() instructions:

for loop

The for() loop is used to run a block of code a specific number of times. It has the following syntax:
for (var i:int=nr_start; i<nr_end; i++) {
  // code to execute
}
Or this:
var i:int;
for (i=nr_start; i<nr_end; i++) {
  // code to execute
}
- var i:int=nr_start; - sets a counter variable and the starting value for our count. This will be the number that counts up during each loop.
- i<nr_end; - represents the condition for the loop to continue running. It sets the ending value for our count.
- i++; - tells the for loop what to do at the end of each loop. In this case increments the value of "i" by 1.

You can use any valid variable name for the name of the counter, but commonly the programmers use "i".


Let's see a simple example with a for loop:
var total:int = 1;

// executes a for loop 4 times
for(var i:int=0; i<4; i++)
{
  // doubles the value of "total" and output it
  total *= 2;
  trace(total);
}

// In Output panel displays:  2 4 8 16
When this script executes the for() loop, first sets the counter variable (var i:int=0). Then, the second statement checks the condition (i<4), which is true and executes the code within curly brackets.
Then, the "i++" statement increments the value of "i" by 1, and it checks again the condition. The condition returns true becouse the value of i is 1, the scripts executes again the code within curly brackets, and so on, the cycle repeats until the condition returns false (in this case when the value of "i" is 4).
In Output panel displays: 2 4 8 16

break instruction

If you want to stop a loop statement before the condition returns false, you can use the break instrucion. This instruction tells the script to end the loop statement, like in the next example:
var total:int = 1;

// executes a for loop 4 times
for(var i:int=0; i<4; i++)
{
  // ends the loop when i=2
  if(i==2) { break; }

  // double the value of "total" and output it
  total *= 2;
  trace(total);
}

// In Output panel displays:  2 4
- The "if(i==2) { break; }" within the for() loop tells the script to end the loop when the value of "i" is 2, even if the condition (i<4) is true. In the Output panel displays: 2 4.
When the loop ends, the other instructions within the for() body, after the "break" statement, are no more executed.

• The for() instruction can be used to loop through every item in an array.
Example:
 // create an Array
var arr:Array = ['site', 'courses', 'tutorials'];

// create a for() instruction to loop through the items in the "arr" Array
for(var i:int=0; i<arr.length; i++)
{
  // displays in Output the value of the current item in "arr"
  trace(arr[i]);
}

// In Output: "site courses tutorials"
 
- In this example, we're looping through every item in the "arr" array, "arr[i]" gets the value of the element with index "i", and the trace() function outputs its value.

for..in loop

The for..in loop is used to iterate over the elements of an Object or an Array. With this statement you can get the name of the properties of an Object (or the indexes of an Array), and perform an operation on each element.
The "for..in" loop has the following syntax:
for (var element:String in anObject) {
  // do some actions
}
- The element variable is the name of the property or function contained in anObject as a string.
For each iteration of the loop, the variable is set to the next property of the object. The loop runs until reaches the last element in "anObject", or until the execution of the loop ends with a "break" instruction.

Let's see an example. We create an object with three properties and use a "for..in" loop to output the name and the value of each property.
// define an object
var obj:Object = {'site':'coursesweb.net', 'gen':'courses', 'web':'tutorials'};

// create a "for..in" loop to iterate over "obj" properties
for(var elm:String in obj)
{
  // use trace() to output the name and the value of the property
  trace(elm+ ' - '+ obj[elm]);
}

// in Output displays:  gen - courses site - coursesweb.net web - tutoriale
The "elm" variable contains the name of the property, "obj[elm]" returns its value.

for each..in

The for each..in loop is slightly different from the "for..in" loop because with "for each..in" you're iterating over property values.
The "for each..in" loop has the following syntax:
for each (var element:Object in anObject) {
  // do some actions
}
The element is the property rather than just the name of the property.

Here is an example. We use the same "obj" object defined in the example above, but we apply a "for each..in" loop.
// define an object
var obj:Object = {'site':'coursesweb.net', 'gen':'courses', 'web':'tutorials'};

// create a "for each..in" loop to iterate over "obj" properties
for each(var elm:Object in obj)
{
  // use trace() to output the value of the property
  trace(elm);
}

// in Output displays:  courses coursesweb.net tutoriale
As you can notice, in this example the "elm" stores the property content (value), while in the "for..in" loop the "elm" contains the property name.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag defines the clickable areas inside the image map?
<map> <img> <area>
<img src="image.jpg" usemap="#map1">
<map name="map1">
  <area shape="rect" coords="9, 120, 56, 149" href="#">
  <area shape="rect" coords="100, 200, 156, 249" href="#">
</map>
Which CSS property defines what is done if the content in a box is too big for its defined space?
display overflow position
#id {
  overflow: auto;
}
Click on the event which is triggered when the mouse is positioned over an object.
onclick onmouseover onmouseout
document.getElementById("id").onmouseover = function(){
  document.write("Have Good Life");
}
Indicate the PHP variable that contains data added in URL address after the "?" character.
$_SESSION $_GET $_POST
if(isset($_GET["id"])) {
  echo $_GET["id"];
}
For loops in ActionScript

Last accessed pages

  1. innerHTML in PHP (5012)
  2. Qwop (39689)
  3. Tic Tac Toe Note Paper (211)
  4. Various Games (2576)
  5. Download JavaScript resources (2996)

Popular pages this month

  1. Qwop (6864)
  2. Drag Racer V3 (4694)
  3. Bubbles3 (1916)
  4. List with All the Games (1605)
  5. Butterfly Kyodai (1507)