Python Loop Control Statements
Loop control statements change execution from their normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements.
Python Continue
It returns the control to the beginning of the loop.
Python3
# Prints all letters except 'e' and 's' for letter in 'w3wiki' : if letter = = 'e' or letter = = 's' : continue print ( 'Current Letter :' , letter) |
Output:
Current Letter : g Current Letter : k Current Letter : f Current Letter : o Current Letter : r Current Letter : g Current Letter : k
Python Break
It brings control out of the loop.
Python3
for letter in 'w3wiki' : # break the loop as soon it sees 'e' # or 's' if letter = = 'e' or letter = = 's' : break print ( 'Current Letter :' , letter) |
Output:
Current Letter : e
Python Pass
We use pass statements to write empty loops. Pass is also used for empty control statements, functions, and classes.
Python3
# An empty loop for letter in 'w3wiki' : pass print ( 'Last Letter :' , letter) |
Output:
Last Letter : s
Exercise: How to print a list in reverse order (from last to the first item) using while and for-in loops.
Loops and Control Statements (continue, break and pass) in Python
Python programming language provides the following types of loops to handle looping requirements.