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
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)
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)
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.
Similar Reads
Loop Control Statements
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:
4 min read
Python Continue Statement
Python Continue Statement skips the execution of the program block after the continue statement and forces the control to start the next iteration. Python Continue StatementPython Continue statement is a loop control statement that forces to execute the next iteration of the loop while skipping the
4 min read
Python - Multi-Line Statements
In this article, we are going to understand the concept of Multi-Line statements in the Python programming language. Statements in Python: In Python, a statement is a logical command that a Python interpreter can read and carry out. It might be an assignment statement or an expression in Python. Mul
3 min read
Python break statement
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 nex
5 min read
Jump Statements in Python
In any programming language, a command written by the programmer for the computer to act is known as a statement. In simple words, a statement can be thought of as an instruction that programmers give to the computer and upon receiving them, the computer acts accordingly. There are various types of
4 min read
Loops and Control Statements (continue, break and pass) in Python
Python supports two types of loops: for loops and while loops. Alongside these loops, Python provides control statements like continue, break, and pass to manage the flow of the loops efficiently. This article will explore these concepts in detail. Table of Content for Loopswhile LoopsControl Statem
2 min read
Python For Loops
Python For Loops are used for iterating over a sequence like lists, tuples, strings, and ranges. For loop allows you to apply the same operation to every item within loop. Using For Loop avoid the need of manually managing the index.For loop can iterate over any iterable object, such as dictionary,
6 min read
Check multiple conditions in if statement - Python
If-else conditional statement is used in Python when a situation leads to two conditions and one of them should hold true. Syntax: if (condition): code1else: code2[on_true] if [expression] else [on_false]Note: For more information, refer to Decision Making in Python (if , if..else, Nested if, if-eli
4 min read
Eliminating Loop from Python Code
In general, Loops are a pillar of any programming language. Loops allow us to execute a set of statements multiple times, which is particularly useful when dealing with lists, arrays, and kind of any of iterable. However, loops especially nested loops can slow down your code and affect the overall p
4 min read
Python Else Loop
Else with loop is used with both while and for loop. The else block is executed at the end of loop means when the given loop condition is false then the else block is executed. So let's see the example of while loop and for loop with else below. Else with While loop Consider the below example. C/C++
3 min read