0

I have code which roughly looks like this:

START  
Recursive method A  
    print("Before Loop")  
    loop:  
    for(Object o: list)  
    {  
          print("Outer loop top")  
       for(Object ob: list2)   
       {  
                 print("Inner loop top")  
          //do stuff with o & ob e.g. recursively call method A under certain conditions  
          if(someCondition true)  
          {  
              print("Entered")  
            break loop;  
          }  
                 print("Inner loop bottom")  
        }   
          print("Outer loop bottom")  
    }  

print("After Loop")  

END  

When the program enters the if statement where someCondition is true it prints

Entered  

and then it seems to break from both for loops successfully as it next prints

After Loop  

AND THEN!! in the console it prints

inner loop bottom  
Outer Loop bottom  
Outer loop top  
inner loop top  
etc.  

How is this possible. method A is called recursively from more then 1 place, BUT, if the the reason "inner loop bottom" was next printed because the method was recursively starting again it wouldn't print ïnner loop bottom' first! but "Before loop" and then "Outer loop top", then "Inner loop top" and THEN "inner loop bottom" would be printed. According to the print statements its breaking from both loops and then jumping back into the bottom of the inner loop and continues to cycle through the inner loop. What is going on. Please help! Thank you

1 Answers1

0

Using break without a label breaks the innermost loop which is currently executing.

Using break with a label foo breaks the statement labeled foo.

https://developer.mozilla.org/en/JavaScript/Reference/Statements/break

The break statement includes an optional label that allows the program to break out of a labeled statement. The break statement needs to be nested within this labelled statement. The labelled statement can be any block statement; it does not have to be preceded by a loop statement.

Credits: what the difference between break with label and without label in javascript

Community
  • 1
  • 1
Hiren Pandya
  • 989
  • 1
  • 7
  • 20
  • thanks for ur reply. ok, well my break statements is nested within the labelled statement, obviously by breaking the outer loop I am breaking inner loop too, so what are you proposing is wrong? I've tried using a break statement without a label to break from the inner loop and then the someCondition=true variable to break from the outer loop. and it prints "After loop" which means it worked, but then it prints "inner loop bottom" again. :/ – user2240342 Apr 03 '13 at 12:53
  • Well this seems a little bit machine dependent.. Because same program on my pc gives the similar but in other order output.. try replacing the place of the label or break without label. But I m afraid that break without label will cause to break out of inner loop only. the similar thing can be done in outer loop.. – Hiren Pandya Apr 03 '13 at 12:58
  • I've tried that, (see my earlier comment) and it responds in the same way :/ – user2240342 Apr 04 '13 at 11:40