Explain the use of break statements in Python

Source: Internet
Author: User

This article mainly introduces the use of break statements in Python, is the introduction of Python exhaled knowledge, the need for friends can refer to the

The break statement in Python terminates the current loop, and continues execution of the next statement, as in the C language.

The most common use of break is when some external conditions are triggered and need to exit from a loop interrupt. Break statements can be used in while and for loops.

If you are using a nested loop (that is, another loop in one loop), the break statement can be used to stop the execution of the inner loop and execute the next line of code in the outer loop.

Grammar

The syntax for the break statement in Python is as follows:

?

1 Break

Flow chart:

?

1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15 #!/usr/bin/python in ' python ': # i-Example if letter = = ' h ': Break print ' ' = # Second Example while var > 0:print ' current variable value: ', var var = var-1 If var = 5:break print ' Go OD bye! "

When the above code is executed, the following results are produced:

?

1 2 3 4 5 6 7 8 9 Current Letter:p current letter:y current letter:t current variable value:10 current variable value:9 current VA Riable value:8 Current variable value:7 current variable value:6 good bye!

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.