Can You Have a Function Loop Through an Object and Through One of Its Values

Looping

Looping statements permit y'all to perform a specific cake of code repeatedly using a series of values or variables. Adobe recommends that you ever enclose the block of code in curly brackets ({}). Although y'all tin omit the curly brackets if the block of code contains just ane statement, this practice is not recommended for the same reason that it is not recommended for conditionals: it increases the likelihood that statements added later on are inadvertently excluded from the block of code. If you afterwards add together a statement that you want to include in the cake of code, but forget to add together the necessary curly brackets, the statement are not executed every bit role of the loop.

for

The for loop allows you to iterate through a variable for a specific range of values. You must supply three expressions in a for statement: a variable that is ready to an initial value, a conditional statement that determines when the looping ends, and an expression that changes the value of the variable with each loop. For instance, the following lawmaking loops five times. The value of the variable i starts at 0 and ends at 4, and the output is the numbers 0 through 4, each on its own line.

var i:int;  for (i = 0; i < v; i++)  {      trace(i);  }

for..in

The for..in loop iterates through the properties of an object, or the elements of an array. For example, you can use a for..in loop to iterate through the properties of a generic object (object properties are non kept in whatever particular order, so backdrop may announced in a seemingly random order):

var myObj:Object = {x:twenty, y:30};  for (var i:String in myObj)  {      trace(i + ": " + myObj[i]);  }  // output:  // x: xx  // y: 30

You can also iterate through the elements of an array:

var myArray:Array = ["one", "2", "three"];  for (var i:String in myArray)  {      trace(myArray[i]);  }  // output:  // one  // two  // three

What you cannot do is iterate through the properties of an object if it is an instance of a sealed class (including built-in classes and user-divers classes). You tin can only iterate through the properties of a dynamic form. Even with instances of dynamic classes, you can only iterate through properties that are added dynamically.

for each..in

The for each..in loop iterates through the items of a drove, which tin be tags in an XML or XMLList object, the values held by object properties, or the elements of an array. For example, equally the post-obit excerpt shows, you can use a for each..in loop to iterate through the properties of a generic object, but unlike the for..in loop, the iterator variable in a for each..in loop contains the value held past the belongings instead of the name of the belongings:

var myObj:Object = {x:20, y:xxx};  for each (var num in myObj)  {      trace(num);  }  // output:  // twenty  // 30

You can iterate through an XML or XMLList object, as the post-obit example shows:

var myXML:XML = <users>      <fname>Jane</fname>      <fname>Susan</fname>      <fname>John</fname>  </users>;    for each (var item in myXML.fname)  {      trace(particular);  }  /* output  Jane  Susan  John  */

Yous tin can also iterate through the elements of an array, as this case shows:

var myArray:Array = ["one", "two", "three"];  for each (var item in myArray)  {      trace(particular);  }  // output:  // 1  // ii  // 3

You cannot iterate through the backdrop of an object if the object is an example of a sealed form. Fifty-fifty for instances of dynamic classes, you cannot iterate through any fixed properties, which are properties defined as part of the class definition.

while

The while loop is like an if statement that repeats as long as the status is true. For example, the following code produces the aforementioned output as the for loop example:

var i:int = 0;  while (i < 5)  {      trace(i);      i++;  }

1 disadvantage of using a while loop instead of a for loop is that infinite loops are easier to write with while loops. The for loop example lawmaking does not compile if you omit the expression that increments the counter variable, but the while loop example does compile if you omit that stride. Without the expression that increments i, the loop becomes an space loop.

do..while

The practise..while loop is a while loop that guarantees that the code block is executed at least once, because the condition is checked later on the code block is executed. The following code shows a unproblematic instance of a practise..while loop that generates output fifty-fifty though the condition is not met:

var i:int = 5;  practice  {      trace(i);      i++;  } while (i < 5);  // output: 5

healytherose.blogspot.com

Source: https://help.adobe.com/en_US/as3/learn/WS5b3ccc516d4fbf351e63e3d118a9b90204-7fcf.html

0 Response to "Can You Have a Function Loop Through an Object and Through One of Its Values"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel