The Wayback Machine - https://web.archive.org/web/20250306100740/https://www.geeksforgeeks.org/break-continue-and-pass-in-python/
Open In App

Loop Control Statements

Last Updated : 09 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Loop control statements in Python are special statements that help control the execution of loops (for or while). They let you modify the default behavior of the loop, such as stopping it early, skipping an iteration, or doing nothing temporarily. . Python supports the following control statements:

Break Statement in Python

The break statement in Python is used to exit or “break” out of a loop (either a for or while loop) prematurely, before the loop has iterated through all its items or reached its condition. When the break statement is executed, the program immediately exits the loop, and the control moves to the next line of code after the loop.

Example:

Python
# Using For Loop
for i in range(5):
    if i == 3:
        break  # Exit the loop when i is 3
    print(i)
    
# Using While Loop
i = 0
while i < 5:
    if i == 3:
        break  # Exit the loop when i is 3
    print(i)
    i += 1

Output
0
1
2
0
1
2

Continue Statement in Python

Python Continue statement is a loop control statement that forces to execute the next iteration of the loop while skipping the rest of the code inside the loop for the current iteration only, i.e. when the continue statement is executed in the loop, the code inside the loop following the continue statement will be skipped for the current iteration and the next iteration of the loop will begin.

Example:

Python
for i in range(5):
    if i == 3:
        continue  # Skip the rest of the code for i = 3
    print(i)

Output
0
1
2
4

Pass Statement in Python

Pass statement in Python is a null operation or a placeholder. It is used when a statement is syntactically required but we don’t want to execute any code. It does nothing but allows us to maintain the structure of our program.

Example: 

Python
for i in range(5):
    if i == 3:
        pass  # Placeholder for future code
    print(i)

Output
0
1
2
3
4

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.



Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg