A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: break within a nested loop

  1. #1
    Junior Member scudsucker's Avatar
    Join Date
    Feb 2003
    Location
    Cape Town, RSA
    Posts
    1,509

    break within a nested loop

    Hi, I just cant wrap my head around thsi one this morning... comapring arrays, adding duplicates to the aDup array, the break seems to beak out of both loops, not just the inner one.
    code:

    for (var i = 0; i<aOld.length; i++) {
    for (var j = 0; j<aNew.length; j++) {
    if (aOld[i] == aNew[j]) {
    aNew[j] = "";// cleared as I only want one positive per aNew entry
    a = [i, aOld[i]];
    aDup.push(a);

    break;
    }
    }
    }




    How do I break only the inner loop, continuing with the outer loop?
    Hariyemadzisawira nhaka yedu! Down the SCUD and win!
    I'm too lazy to read Private Messages.

  2. #2
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    break always breaks the current (latest) loop it is in, and not the parent loop.

    in this example only the second loop (with the j's) is affected by the break.

    code:

    for (i=0;i<10;i++) {
    for (j=0;j<10;j++) {
    if(j>1) {
    trace("brk"+j);
    break;
    };
    trace("j="+j);
    }
    trace("i="+i);
    }



    Try tracing your 1st (i) loop to prove whether it is being stopped or not.
    www.lexicon-design.co.uk
    If we aren't supposed to eat animals, then why are they made of meat?
    If Vegetarians like animals so much, why do they eat all their food?

  3. #3
    Junior Member scudsucker's Avatar
    Join Date
    Feb 2003
    Location
    Cape Town, RSA
    Posts
    1,509
    Cheers, Lexicon.. I posted simplified code, and have gone over my real code, and found the error.

    Monday morning....... thanks for your help.
    Hariyemadzisawira nhaka yedu! Down the SCUD and win!
    I'm too lazy to read Private Messages.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center