The break statement described in this article is like in the C language, breaking the minimum enclosing
For or while loop。
Break 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 statements。
Break 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