This article describes how to use the break statement in Python. It is an introduction to outbound calling in Python. If you need it, you can refer to the break statement in Python to terminate the current loop and continue to execute the next statement, just like break in C.
The most common purpose of break is to exit from a loop when some external conditions are triggered. The break statement can be used in the while and for loop.
If you are using a nested loop (that is, embedding another loop in one loop), the break statement can be used to stop the execution of the innermost loop and execute the next line of code in the outer loop.
Syntax
In Python, the break statement syntax is as follows:
Break
Flowchart:
#! /Usr/bin/pythonfor letter in 'python': # First Example if letter = 'H': break print 'current Letter :', letter var = 10 # Second Examplewhile var> 0: print 'current variable value: ', var = var-1 if var = 5: breakprint "Good bye! "
When the above code is executed, the following results are generated:
Current Letter: PCurrent Letter: yCurrent Letter: tCurrent variable value: 10 Current variable value: 9 Current variable value: 8 Current variable value: 7 Current variable value: 6 Good bye!