break, continue and pass in Python
Using loops in Python automates and repeats the tasks in an efficient manner. But sometimes, there may arise a condition where you want to exit the loop completely, skip an iteration or ignore that condition. These can be done by 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:
Break Statement in Python
The break statement in Python is used to terminate the loop or statement in which it is present. After that, the control will pass to the statements that are present after the break statement, if available. If the break statement is present in the nested loop, then it terminates only those loops which contain the break statement.
Syntax of Break Statement
The break statement in Python has the following syntax:
for / while loop:
# statement(s)
if condition:
break
# statement(s)
# loop end
Working on Python Break Statement
The working of the break statement in Python is depicted in the following flowchart:
Working of Python Break Statement
Example:
In this example, we will see the usage of break statements with Python loops and strings. First, we will use the break statement with for loops to exit out of the for loop when specific characters are encountered in the string. Then we will perform the same operation but with the while loop.
Python3
s = 'geeksforgeeks'
for letter in s:
print (letter)
if letter = = 'e' or letter = = 's' :
break
print ( "Out of for loop" )
print ()
i = 0
while True :
print (s[i])
if s[i] = = 'e' or s[i] = = 's' :
break
i + = 1
print ( "Out of while loop" )
|
Output:
g
e
Out of for loop
g
e
Out of while loop
Example:
In this example, we will see the usage of break statements with nested for loops in Python. If the break statement is declared within the inner for loop, then the control comes out of only that loop. The outer for loops still continues to work.
Python3
for i in range ( 1 , 5 ):
for j in range ( 2 , 6 ):
if j % i = = 0 :
break
print (i, " " , j)
|
Output:
3 2
4 2
4 3
Continue Statement in Python
Continue is also a loop control statement just like the break statement. continue statement is opposite to that of the break statement, instead of terminating the loop, it forces to execute the next iteration of the loop. As the name suggests the continue statement forces the loop to continue or execute the next iteration. When the continue statement is executed in the loop, the code inside the loop following the continue statement will be skipped and the next iteration of the loop will begin.
Syntax of Continue Statement
The continue statement in Python has the following syntax:
for / while loop:
# statement(s)
if condition:
continue
# statement(s)
Working of Python Continue Statement
The working of the continue statement in Python is depicted in the following flowchart:
Working of Python Continue Statement
Example:
In this example, we will use Python continue statement with for loop to iterate through a range of numbers and to continue to the next iteration without performing the operation on that particular element when some condition is met.
Python3
for i in range ( 1 , 11 ):
if i = = 6 :
continue
else :
print (i, end = " " )
|
Output:
1 2 3 4 5 7 8 9 10
Pass Statement in Python
As the name suggests pass statement simply does nothing. The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute. It is like a null operation, as nothing will happen if it is executed. Pass statements can also be used for writing empty loops. Pass is also used for empty control statements, functions, and classes.
Syntax of Pass Statement
The pass statement in Python has the following syntax:
function/ condition / loop:
pass
Example:
In this example, we will use the pass statement with an empty for loop and an empty Python function. We just declared a function and write the pass statement in it. When we try to call this function, it will execute and not generate an error.
Then we use the pass statement with an if condition within a for loop. When the value of “i” becomes equal to ‘k’, the pass statement did nothing, and hence the letter ‘k’ is printed.
Python3
s = "geeks"
for i in s:
pass
def fun():
pass
fun()
for i in s:
if i = = 'k' :
print ( 'Pass executed' )
pass
print (i)
|
Output:
g
e
e
Pass executed
k
s
Last Updated :
14 Jul, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...