Python break statement details

  • 2020-04-02 13:32:51
  • OfStack

The Python break statement, as in C, breaks the minimal closed for or while loop. The break statement is used to terminate the loop statement, that is, the loop condition does not have a False condition or the sequence has not been completely recursed, and the loop statement is also stopped.
The break statement is used in the while and for loops. If you use nested loops, the break statement stops the deepest loop and starts the next line of code.

The syntax of Python break statement

break

Second, the logic flow chart
< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / files.jb51.net/file_images/article/201403/2014311174657180.jpg? 2014211175019 ">
Use examples

#!/usr/bin/python
for letter in 'Python':     # First Example
   if letter == 'h':
      break
   print 'Current Letter :', letter

var = 10                    # Second Example
while var > 0:              
   print 'Current variable value :', var
   var = var -1
   if var == 5:
      break
print "Good bye!"

Execution results of the above examples:
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!


Related articles: