Tuesday, May 24, 2011

About for, for..in and for each..in Statement

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 set 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 example, the following code loops five times. The value of the variable i starts at 0 and ends at 4, and the output will be the numbers 0 through 4, each on its own line.

var i:int;
for (i = 0; i < 5; 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 not kept in any particular order, so properties may appear in a seemingly random order):

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

// output: 
// x: 20
// y: 30 

You can also iterate through the elements of an array:
var myArray:Array = ["one", "two", "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 user-defined class, unless the class is a dynamic class. Even with instances of dynamic classes, you will be able to iterate only through properties that are added dynamically.
 
 
for each..in 

The for each..in loop iterates through the items of a collection, which can be tags in an XML or XMLList object, the values held by object properties, or the elements of an array. For example, as the following 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 by the property instead of the name of the property:

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

You can iterate through an XML or XMLList object, as the following example shows:

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

/* output Jane Susan John */ 

You can also iterate through the elements of an array, as this example shows:

var myArray:Array = ["one", "two", "three"]; 

for each (var item in myArray) 
{
trace(item);
}

// output: 
// one
// two 
// three

You cannot iterate through the properties of an object if the object is an instance of a sealed class. Even for instances of dynamic classes, you cannot iterate through any fixed properties, which are properties defined as part of the class definition.


For Example

MXML File

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script
        >
        <![CDATA[
            import mx.controls.Alert;
           
            public function foreachfun():void
            {
                var myArray:Array = ["one", "two", "three"];
                   
                    for each (var item in myArray)
                    {
                        Alert.show(item);
                    }
                   
                }
               
                public function forloopfun():void
                {
                    var myArray:Array = ["one", "two", "three"];
                       
                        for (var i:String in myArray)
                        {
                            Alert.show(myArray[i]);
                        }

                }
                public function xmlfileforeach():void
                {
                    var myXML:XML = <users>
                     <fname>Jane</fname>
                     <fname>Susan</fname>
                     <fname>John</fname>
                      <fname>Susan1</fname>
                     <fname>John1</fname>
                    </users>;
                   
                    for each (var item in myXML.fname)
                    {
                        Alert.show(item);
                    }
                }
               
                public function custom():void
                {
                    var sam:Array=new Array("kaniskar","Banupriya","Banu");
                   
                    for each(var j in sam)
                    {
                        Alert.show(j);
                       
                        if(j=="Banupriya")
                        {
                            trace("True");
                            break;
                        }
                    }
                }
               
               
        ]]>
    </mx:Script>
   
    <mx:Button x="378" y="397" label="For each Loop" click="foreachfun();"/>
    <mx:Button x="378" y="427" label="For Loop" click="forloopfun();"/>
    <mx:Button x="378" y="457" label="For each" click="xmlfileforeach();"/>
        <mx:Button x="378" y="487" label="Break" click="custom();"/>
   
</mx:Application>