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 in <table> to create table header cell?
<thead> <th> <td>
<table><tr>
  <th>Title 1</th>
  <th>Title 2</th>
</tr></table>
Which CSS property sets the distance between lines?
line-height word-spacing margin
.some_class {
  line-height: 150%;
}
Which function opens a new browser window.
alert() confirm() open()
document.getElementById("id_button").onclick = function(){
  window.open("http://coursesweb.net/");
}
Indicate the PHP function that returns an array with names of the files and folders inside a directory.
mkdir() scandir() readdir()
$ar_dir = scandir("dir_name");
var_export($ar_dir);
For loops in ActionScript

Last accessed pages

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142540)
  2. JavaScript strip_tags and stripslashes (8825)
  3. Date and Time in ActionScript 3 (10086)
  4. Bind Tool and Control Points (4430)
  5. Clear Canvas Context (8116)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (543)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (63)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)