For loops can be useful for repeating an action multiple times.
In ActionScript we can use the following repetitive for() instructions:
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.
You can use any valid variable name for the name of the counter, but commonly the programmers use "i".
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 16When 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.
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.
// 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 (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.
// 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 - tutorialeThe "elm" variable contains the name of the property, "obj[elm]" returns its value.
for each (var element:Object in anObject) { // do some actions }The element is the property rather than just the name of the property.
// 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 tutorialeAs 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.
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net