Python break statement

Python break statements, like in C, break the minimal closed for or while loop.

The break statement is used to terminate the loop statement. That is, if the loop condition has no False condition or the sequence has not been completely recursed, the loop statement will be stopped.

The break statement is used in the while and for loops.

If you use nested loops, the break statement will stop executing the deepest loop and start executing the next line of code.

Python language break statement syntax:



Instance(Python 2.0+)

#!/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 the loop when the variable var is equal to 5 break print "Good bye!"

The above example execution results:

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!

javacodegeeks is optimized for learning.© javacodegeeks
All Right Reserved and you agree to have read and accepted our term and condition.