The best approach I know to continue an outer loop is using a Boolean that is scoped under the outer loop and breaking the inner one.
You can break all loops with else and continue . The code with explanation is as follows. When the inner loop ends normally without break , continue in the else clause is executed. This continue is for the outer loop, and skips break in the outer loop and continues to the next cycle.
The continue keyword can be used in any of the loop control structures. It causes the loop to immediately jump to the next iteration of the loop. In a for loop, the continue keyword causes control to immediately jump to the update statement.
Another way to skip nested loop cycles is with C#'s continue statement and the break statement. With the first we can continue the outer loop, and the second can stop inner loops. When we execute the continue statement inside a loop, code execution immediately goes to that loop's next loop cycle (Microsoft Docs, 2017).
for ii in range(200):
for jj in range(200, 400):
...block0...
if something:
break
else:
...block1...
Break
will break the inner loop, and block1 won't be executed (it will run only if the inner loop is exited normally).
for i in ...:
for j in ...:
for k in ...:
if something:
# continue loop i
In a general case, when you have multiple levels of looping and break
does not work for you (because you want to continue one of the upper loops, not the one right above the current one), you can do one of the following
def inner():
for j in ...:
for k in ...:
if something:
return
for i in ...:
inner()
The disadvantage is that you may need to pass to that new function some variables, which were previously in scope. You can either just pass them as parameters, make them instance variables on an object (create a new object just for this function, if it makes sense), or global variables, singletons, whatever (ehm, ehm).
Or you can define inner
as a nested function and let it just capture what it needs (may be slower?)
for i in ...:
def inner():
for j in ...:
for k in ...:
if something:
return
inner()
Philosophically, this is what exceptions are for, breaking the program flow through the structured programming building blocks (if, for, while) when necessary.
The advantage is that you don't have to break the single piece of code into multiple parts. This is good if it is some kind of computation that you are designing while writing it in Python. Introducing abstractions at this early point may slow you down.
Bad thing with this approach is that interpreter/compiler authors usually assume that exceptions are exceptional and optimize for them accordingly.
class ContinueI(Exception):
pass
continue_i = ContinueI()
for i in ...:
try:
for j in ...:
for k in ...:
if something:
raise continue_i
except ContinueI:
continue
Create a special exception class for this, so that you don't risk accidentally silencing some other exception.
I am sure there are still other solutions.
In other languages you can label the loop and break from the labelled loop. Python Enhancement Proposal (PEP) 3136 suggested adding these to Python but Guido rejected it:
However, I'm rejecting it on the basis that code so complicated to require this feature is very rare. In most cases there are existing work-arounds that produce clean code, for example using 'return'. While I'm sure there are some (rare) real cases where clarity of the code would suffer from a refactoring that makes it possible to use return, this is offset by two issues:
The complexity added to the language, permanently. This affects not only all Python implementations, but also every source analysis tool, plus of course all documentation for the language.
My expectation that the feature will be abused more than it will be used right, leading to a net decrease in code clarity (measured across all Python code written henceforth). Lazy programmers are everywhere, and before you know it you have an incredible mess on your hands of unintelligible code.
So if that's what you were hoping for you're out of luck, but look at one of the other answers as there are good options there.
I think you could do something like this:
for ii in range(200):
restart = False
for jj in range(200, 400):
...block0...
if something:
restart = True
break
if restart:
continue
...block1...
We want to find something and then stop the inner iteration. I use a flag system.
for l in f:
flag = True
for e in r:
if flag==False:continue
if somecondition:
do_something()
flag=False
I think one of the easiest ways to achieve this is to replace "continue" with "break" statement,i.e.
for ii in range(200):
for jj in range(200, 400):
...block0...
if something:
break
...block1...
For example, here is the easy code to see how exactly it goes on:
for i in range(10):
print("doing outer loop")
print("i=",i)
for p in range(10):
print("doing inner loop")
print("p=",p)
if p==3:
print("breaking from inner loop")
break
print("doing some code in outer loop")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With