#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    26
    Rep Power
    0

    Exit multiple loops


    How do I break out of an inner loop, i.e. break out of all outer loops from the inner one

    Code:
    for k in listOne:
         for j in listTwo:
             #break here out of both loops
    
    #continue here after break
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    I am against breaking out of loops. The reason is because it is difficult to read code that uses breaks. It is much clearer to use a boolean value to evaluate whether a condition is true or not.

    e.g.
    Code:
    boolean isFound = false;
    int index = 0;
    while !isFound:
        if value == key:
            isFound = true;
    You can use the break statement to break out of a for or while block of code.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    26
    Rep Power
    0
    Ok...but there's no primitive that lets you exit all outer loops...
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    This feature was proposed and rejected. It would have allowed you to specify a memory address and break or continue on that memory address. Look under the motivation section of the link to see examples of how you break out of multiple loops in Python.
    http://www.python.org/dev/peps/pep-3136/

    Writing code like the above leads itself to being a code smell. I don't recommend it. You are basically "hacking" a solution which is never good. I would re-think my design if my code started to look like this.

    Comments on this post

    • zxq9 agrees : Absolutely. +1 on the PEP reference.
    Last edited by Cameron0960; July 23rd, 2013 at 04:46 PM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    Usually Japan when not on contract
    Posts
    240
    Rep Power
    12
    As a follow-on to what Cameron0960 had to say...

    What is the general problem you are trying to solve where you need to break out of a nested loop? If you show us what you are trying to accomplish maybe someone around here can show you a cleaner (maybe even pythonic) way to get it done.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    475
    Rep Power
    33
    The usual way is to use a function
    Code:
    def call_loops()
        for k in listOne:
            for j in listTwo:
                #break here out of both loops
                if some_condition:
                    return
    
    call_loops()
    print "finished loops"
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    26
    Rep Power
    0
    Thanks guys, I just have a situation where I don't want to return from the method when I break from an inner loop. I want to continue execution on the next line right after the outer-most loop.

    I could move the loop to a different function that exits when I need to break. I was just wondering if there was any other way
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    Usually Japan when not on contract
    Posts
    240
    Rep Power
    12
    There probably is another way, and it involves finding a different design. That's what most of the previous posts were about. We can't help you do that if you stay focused on the accidental detail of how to break a nested loop instead of explaining what the essential problem is you are trying to solve (for example, I'm trying to derive partial-period percentages in a list of transactions, or make a game character able to change direction mid-jump or whatever).

IMN logo majestic logo threadwatch logo seochat tools logo