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

    Conditional Looping

    flCoder74 Level 1

      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
          kkc_mca Level 2

          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

               }

          }

           

          if this post answers your question or helps. Please mark it as such

          • 2. Re: Conditional Looping
            flCoder74 Level 1

            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
              paul.williams Level 4

              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).

               

              So migrating your if statement into your termination condition does not

              make sense to me. What are you expecting to happen?

              1 person found this helpful
              • 4. Re: Conditional Looping
                flCoder74 Level 1

                Thank you! That makes sense. I wasn't thinking that it was stopping the loop when it evaluated true, but that it just skipped that item and examined the next.