Loops in Python

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.

Syntax:

for variable in sequence:
    statement(s)
Example:

for i in range(5):
    print("Iteration:", i)
Output:

Iteration: 0  
Iteration: 1  
Iteration: 2  
Iteration: 3  
Iteration: 4

Example โ€“ Iterating over a list:

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)
Output:

apple  
banana  
cherry
The range() function generates a sequence of numbers โ€” commonly used in for loops.

while Loop

The while loop executes a block of code as long as a condition remains True. When the condition becomes False, the loop stops.

Syntax:

while condition:
    statement(s)
Example:

count = 1
while count <= 5:
    print("Count:", count)
    count += 1
Output:

Count: 1  
Count: 2  
Count: 3  
Count: 4  
Count: 5
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!")
Output:

Iteration 0  
Iteration 1  
Iteration 2  
Loop completed successfully!

Example 2: for-else with break

for i in range(3):
    if i == 1:
        break
    print("Iteration", i)
else:
    print("Loop completed successfully!")
Output:

Iteration 0
Notice that the else block did not execute because the loop ended using break.

Nested Loops

You can use loops inside other loops, called nested loops. For example, a for loop inside another for loop.

Example:

for i in range(1, 4):
    for j in range(1, 3):
        print(f"i={i}, j={j}")
Output:

i=1, j=1  
i=1, j=2  
i=2, j=1  
i=2, j=2  
i=3, j=1  
i=3, j=2

Summary

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.
Share this Article