Actionscript Course

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 is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
For loops in ActionScript

Last accessed pages

  1. Shape Tween - Flash Animation (6149)
  2. The Mastery of Love (7440)
  3. Get Mime Type of file or string content in PHP (6230)
  4. Countdown Timer with starting time added into a form (11533)
  5. Disable button and Enable it after specified time (17533)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (477)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (72)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (64)
  5. CSS3 2D transforms (46)