The Python break statement is detailed

Source: Internet
Author: User
The Python break statement, like in the C language, breaks the minimum enclosing for or while loop. The break statement terminates the Loop statement, that is, the loop condition does not have a false condition, or the sequence is not fully recursive, and the loop statement is stopped.
The break statement is used in the while and for loops. If you use a nested loop, the break statement stops executing the deepest loop and starts executing the next line of code.

One, the Python language break statement syntax
Copy the Code code as follows:

Break

Second, logical flowchart

Iii. Examples of Use
Copy the Code code as follows:

#!/usr/bin/python

For letter in ' Python ': # First Example
If letter = = ' h ':
Break
print ' Current letter: ', letter

var = ten # Second Example
While var > 0:
print ' current variable value: ', var
var = var-1
if var = = 5:
Break

Print "Good bye!"


The result of the above instance execution:
Copy CodeThe code is as follows:

Current Letter:p
Current Letter:y
Current Letter:t
Current variable Value:10
Current variable Value:9
Current variable 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.