The Python break statement, like in the C language, breaks the minimal enclosing for or while loop. The break statement terminates the Loop statement, which means that the loop condition does not have the false condition or the sequence is not fully recursive, and also stops execution of the loop statement.
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.
A, Python language break statement syntax
Copy Code code as follows:
Second, the logic flow chart
Iii. Use of examples
Copy Code code as follows:
#!/usr/bin/python
For letter in ' Python ': # 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 above example performs the result:
Copy Code code 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!