break, continue and pass in Python
Last Updated :
12 Aug, 2024
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.
Python
# Python program to demonstrate
# break statement
s = 'geeksforgeeks'
# Using for loop
for letter in s:
print(letter)
# break the loop as soon it sees 'e'
# or 's'
if letter == 'e' or letter == 's':
break
print("Out of for loop")
print()
i = 0
# Using while loop
while True:
print(s[i])
# break the loop as soon it sees 'e'
# or 's'
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.
Python
# Python program to demonstrate
# break statement with nested
# for loop
# first for loop
for i in range(1, 5):
# second for loop
for j in range(2, 6):
# break the loop if
# j is divisible by i
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.
Python
# Python program to
# demonstrate continue
# statement
# loop from 1 to 10
for i in range(1, 11):
# If i is equals to 6,
# continue to next iteration
# without printing
if i == 6:
continue
else:
# otherwise print the value
# of i
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.
Python
# Python program to demonstrate
# pass statement
s = "geeks"
# Empty loop
for i in s:
# No error will be raised
pass
# Empty function
def fun():
pass
# No error will be raised
fun()
# Pass statement
for i in s:
if i == 'k':
print('Pass executed')
pass
print(i)
Output:
g
e
e
Pass executed
k
s
break, continue and pass in Python – FAQs
What is Break, Continue, and Pass in Python?
Break: The break
statement in Python is used to exit a loop prematurely, regardless of the condition of the loop. When Python encounters a break
statement, it immediately terminates the loop and proceeds with the next line of code outside the loop.
Continue: The continue
statement in Python is used to skip the remainder of the code inside a loop for the current iteration only. The loop does not terminate but proceeds to the next iteration.
Pass: The pass
statement in Python is a null operation; nothing happens when it is executed. It is used as a placeholder in a block where Python expects an expression.
What is the Difference Between Continue and Break in Python for Loop?
The continue
statement skips the rest of the code inside the loop for the current iteration and moves to the next iteration of the loop. The break
statement, on the other hand, completely terminates the loop and transfers execution to the code immediately following the loop.
Example:
for i in range(5):
if i == 3:
continue
print(i) # This will print 0, 1, 2, 4
for i in range(5):
if i == 3:
break
print(i) # This will print 0, 1, 2
What is the Difference Between Break and Quit in Python?
The break
statement is used to exit a loop, while quit()
is a built-in Python function that exits from Python itself, closing down the Python interpreter. quit()
is typically used in interactive sessions rather than in production code.
What is __init__
in Python?
__init__
is a special method in Python, commonly known as the constructor. It is automatically called when a new instance of a class is created. The main purpose of __init__
is to initialize the attributes of the class for the new object by assigning values to them. This method may accept arguments to pass data to initialize the object.
Example:
class Car:
def __init__(self, make, model):
self.make = make
self.model = model
car1 = Car("Toyota", "Corolla")
print(car1.make) # Outputs: Toyota
print(car1.model) # Outputs: Corolla
This setup of using break
, continue
, and pass
provides control over loop execution, while __init__
serves as a fundamental part of class definition in Python, crucial for initializing new objects.