break two for loops
Question: > **Possible Duplicate:**  
>  [How to break out of multiple loops in
> Python?](http://stackoverflow.com/questions/189645/how-to-break-out-of-
> multiple-loops-in-python)

is it possible to break out of two **for** loops in Python?  
i.e.

    
    
    for i in range(1,100):  
        for j in range(1,100):  
            break ALL the loops!
    


Answer: No, there is no nested `break` statement in python.

Instead, you can simplify your function, like this:

    
    
    import itertools
    for i,j in itertools.product(range(1, 100), repeat=2):
      break
    

.. or put the code into its own function, and use `return`:

    
    
    def _helper():
       for i in range(1,100):
           for j in range(1,100):
               return
    _helper()
    

.. or use an exception:

    
    
    class BreakAllTheLoops(BaseException): pass
    try:
       for i in range(1,100):
           for j in range(1,100):
               raise BreakAllTheLoops()
    except BreakAllTheLoops:
        pass
    

.. or use for-else-continue:

    
    
    for i in range(1,100):
        for j in range(1,100):
           break
        else:
           continue
        break
    

.. or use a flag variable:

    
    
    exitFlag = False
    for i in range(1,100):
        for j in range(1,100):
            exitFlag = True
            break
        if exitFlag:
            break
    

