|
-
Junior Member
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.
-
Monkey Moderator
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?
-
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|