for a inch xrange (ten): for b inch xrange (): if something (a,b): # Break the inner loop ... Break Else : # Continue If the inner loop wasn ' t broken. Continue # Inner loop is broken, break the outer. Break
Reply content:
I'm a little tempted to say ... Do you learn Python never look at the official documents, just follow a certain Chinese course to go all the way?
Python's loop body has the Else branch itself!
Python's loop body has the Else branch itself!
Python's loop body has the Else branch itself!
Not just if, while and for have an else branch.
the Else branch trigger condition of the loop body is that the loop ends normally。 If a break jumps inside the loop, the else is not executed. So this logic is: if the loop inside break, do not trigger else, then the next sentence of the outer loop of break; If the normal end, execute the continue in the Else branch, jump directly to the next round of the outer loop, skip the second break.
But it's Nima. Do a Boolean will die ...
————————
In fact, the more correct posture should be to take a function wrap up and then return ... Throw an exception. The ELSE clause for and while is used for loop post processing, only when the loop ends normally.
The example given in the question, setting a flag variable, the code will look more intuitive.
I'd like to use the Else branch to say which is good.