Where can the break statement be used? Daniel tells you the effect of break in the loop statement

Source: Internet
Author: User
The break statement described in this article is like in the C language, breaking the minimum enclosing For or while loopBreak StatementUsed to terminate Looping Statements, that is, the loop condition does not have a false condition or the sequence has not been fully recursive, it will Stop execution of loop statementsBreak StatementUsed 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.





Break Statement The syntax :


Break


Flow chart:






Instance:


#! / usr / bin / python
#-*-coding: UTF-8-*-
  for letter in 'Python': # First instance
    if letter == 'h':
       break
    print 'current letter:', letter
      
   var = 10 # second instance
while var> 0:
    print 'Current variable value:', var
    var = var -1
    if var == 5: # exit loop when variable var equals 5
       break
  print "Good bye!" 


The result of the above instance execution:



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!



Knowledge points related to this article:



What does the pass statement do in a python statement? On the usage of pass statement



python continue statement? The usage of the Python continue statement is detailed


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.