How do I use break to jump out of a multilayer loop in Python?

Source: Internet
Author: User
 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.
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.