Nagesh Chauhan22 Mar 2026, Updated: 09 Apr 20264 min read0
In programming, loops are used to execute a block of code repeatedly until a specific condition is met.
Python provides two main types of loops: the for loop and the while loop.
In this article, we'll cover how loops work in Python, how to use break, continue, and pass statements, and the unique else clause that can be used with loops.
What is a Loop?
A loop allows you to execute a block of code multiple times. It helps automate repetitive tasks such as processing lists, iterating through numbers, or performing calculations until a condition changes.
for Loop
The for loop is used to iterate over a sequence (like a list, tuple, string, or range of numbers). Python's for loop works differently than in many other languages โ it directly iterates over items rather than using an index counter.
Always ensure the condition eventually becomes False, otherwise it leads to an infinite loop.
Loop Control Statements
Python provides three special statements to alter the normal loop behavior:
Statement
Description
break
Exits the loop completely
continue
Skips the current iteration and moves to the next
pass
Does nothing โ acts as a placeholder
'break' Statement
The break statement immediately terminates the loop when a specific condition is met.
Example:
for i in range(1, 10):
if i == 5:
break
print(i)
Output:
1
2
3
4
The loop stops when i equals 5.
'continue' Statement
The continue statement skips the rest of the current iteration and moves to the next one.
Example:
for i in range(1, 6):
if i == 3:
continue
print(i)
Output:
1
2
4
5
When i equals 3, the loop skips printing and continues with the next iteration.
'pass' Statement
The pass statement does nothing. It's used as a placeholder where code will be added later or where an empty block is syntactically required.
Example:
for i in range(5):
pass # To be implemented later
The loop runs but performs no action.
else Clause with Loops
Python provides a unique feature โ an else clause that can be used with both for and while loops. The else block executes only if the loop completes normally (i.e., not terminated by a break statement).
Example 1: for-else
for i in range(3):
print("Iteration", i)
else:
print("Loop completed successfully!")
Loops are the heart of automation in Python โ enabling repetition, iteration, and efficient logic building. Understanding how to use for, while, and control statements like break, continue, and pass gives you the power to write cleaner and more dynamic programs.
Concept
Description
for loop
Iterates over a sequence (list, string, range, etc.)
while loop
Repeats code while a condition is True
break
Terminates the loop immediately
continue
Skips to the next iteration
pass
Placeholder โ does nothing
loop else
Executes after a loop ends normally (no break)
In the next article, weโll explore Functions in Python โ how to define, call, and return values from reusable code blocks.
Principal Engineer | Java ยท Spring Boot ยท Python ยท Microservices ยท AI/ML
Principal Engineer with 14+ years of experience in designing scalable systems using
Java, Spring Boot, and Python. Specialized in microservices architecture, system
design, and machine learning.
Join the discussion