4 Replies Latest reply on Aug 23, 2009 4:03 PM by flCoder74

Conditional Looping

I have a looping structure like this:

for (var ii:int = 0; i < i; i++) {

for (var ii:int = 0; ii < i && myAC[ii].apID != myAC[i].apID; ii++) {

if (myAC[ii].Group == myAC[i].Group) {

//does something

}

}

}

and I get the results I expect. However, I would like to be more restrictive in the loop, and do this:

for (var ii:int = 0; i < i; i++) {

for (var ii:int = 0; ii < i && myAC[ii].apID != myAC[i].apID && myAC[ii].Group == myAC[i].Group; ii++) {

//does nothing

}

}

Adding the second && criteria in the inner loop makes it so that it never evaluates to true.  Is there a way to do this?

• 1. Re: Conditional Looping

check by placing the condition in paranthesis

for (var ii:int = 0; i < i; i++) {

for (var ii:int = 0 ; (ii < i )&&( myAC[ii].apID != myAC[i].apID ) && (myAC[ii].Group == myAC[i].Group) ; ii++) {

//does nothing

}

}

• 2. Re: Conditional Looping

Thanks for the reply, but this doesn't work either, I get no results, where if I put the && conditions as an if statement in the loop, it works as expected.

for (var ii:int = 0 ; (ii < i )&&( myAC[ii].apID != myAC[i].apID ) && (myAC[ii].Group == myAC[i].Group) ; ii++) {

• 3. Re: Conditional Looping

Puttng an if statement inside the loop is very different from adding

expressions to the termination condition of the for statement. The

termination condition determines when the loop will exit. As soon as

that expression evalulates to false, your loop will exit. The if

statement in your example allows you to conditionally perform actions

for a particular iteration before moving onto the next iteration. The if

statement will not cause your loop to exit if it evaluates to false

(unless you use a break statement).