24 March 2009

Advanced For Loop Actionscript Example

Let's create two arrays.

var a:Array = [3, 5, 7, 9];
var b:Array = [2, 3, 7, 9, 11];

Next function will check if arrays have equal elements on same places.


function checkArrays(arrayA:Array, arrayB:Array):void {
for(var i=0, j=0; (i<arrayA.length-1) || (j<arrayB.length-1); i++, j++) {
if(arrayA[i]==arrayB[j]){
trace("elements a["+i+"] and b["+j+"] are equal!");
trace("element value is : "+arrayA[i]);
}
}
trace("process finished.");
}

checkArrays(a, b);

same works in as2.0, but instead of returned type void as2.0 prefers Void with capital letter V.

output ::

elements a[2] and b[2] are equal!
element value is : 7
elements a[3] and b[3] are equal!
element value is : 9
process finished.

This for loop will finish in 4 iterations, when first array runs out of elements.

This example is simple to understand and in fact it is little unusual one since it needs single variable to find same elements and not both i and j. We can check arrays in 'reverse' order also, meaning if first element of array a is equal to last element of array b, etc. Here is the code:


function checkArrays2(arrayA:Array, arrayB:Array):void {
for(var i=0, j=arrayB.length-1; (i<arrayA.length-1) || (j>0); i++, j--) {
if(arrayA[i]==arrayB[j]){
trace("elements a["+i+"] and b["+j+"] are equal!");
trace("element value is : "+arrayA[i]);
}
}
trace("process finished.");
}

and output is ::

elements a[2] and b[2] are equal!
element value is : 7
process finished.

In this example, first iteration checks if a[0] and b[4] are equal and nothing happens because 3 is not 11. Second iteration checks a[1] and b[3], third checks a[2] and b[2], this one will produce output and last iteration checks a[3] and b[1]. After this step for loop ends because i becomes equal to arrayA.length-1. This means that element b[0] is never checked because arrays don't have same length.

Point is you can use multiple initial values, complex loop exit condition and multiple iteration steps inside the for loop and do more with less code. I hope you find this example useful.

.

3 comments:

CodeMann said...

one variable is ok, no need to use a j:
function checkArrays(arrayA:Array, arrayB:Array):void {
for(var i=0; (i<arrayA.length) || (i&ltarrayB.length); i++) {
if(arrayA[i]==arrayB[i]){
trace("elements a["+i+"] and b["+i+"] are equal!");
trace("element value is : "+arrayA[i]);
}
}

and I would like to code in this way, it runs faster:
function checkArrays(arrayA:Array, arrayB:Array):void {
var len:int=arrayA.length<arrayB.length?arrayA.length:arrayB.length;
for(var i:int=0;i<len;i++)
if(arrayA[i]==arrayB[i]){
trace("elements a["+i+"] and b["+i+"] are equal!");
trace("element value is : "+arrayA[i]);
}
}

iluvtyranny said...

Hi... I'm new to Actionscript and find this code intriguing and slightly confusing.. I was wondering there is a -1 instead of just the values in that bit of code?

diseƱo web said...

I have reviewed your Web site and has very good resources like this one. Thanks to put at the disposal of all these subjects

 

template by blogger templates