Python else Clause in Loops - Master for and while else with Examples
Learn how to use the else clause with Python loops. Explore for and while else with examples, syntax, and practical use cases for better control flow understanding..
Table of Contents
- The else Clauses on Loops
- Syntax for Python
elseClause in Loops - Example with a
forloop - Example with a
whileloop
1. The else Clauses on Loops
In Python, the else clause can be used with loops (for and while). This may be surprising at first since most people associate else with if statements. However, in loops, the else clause has a unique behavior:
- The
elseblock is executed only if the loop completes all its iterations without encountering abreakstatement. - If the loop is exited early because of a
break, theelseblock is skipped.
The else clause in loops (for and while) in Python is a bit unusual because most people associate else with if statements. In the context of loops, the else clause is executed only when the loop finishes normally, meaning it wasnβt interrupted by a break statement.
2. Syntax for Python else Clause in Loops
The else clause can be used with both for and while loops in Python. Here's the general syntax:
For Loop with else
for item in iterable:
# Code block to execute for each item
if condition:
break # Exit the loop early
else:
# Code block to execute if the loop completes without a breakWhile Loop with else
while condition:
# Code block to execute while the condition is True
if condition_to_break:
break # Exit the loop early
else:
# Code block to execute if the loop completes without a breakHow It Works:
-
With a
forloop:- The
elseblock runs if the loop completes all its iterations without hitting abreak. - If the loop is terminated by a
break, theelseblock is skipped.
- The
-
With a
whileloop:- The
elseblock runs if thewhileloop condition becomesFalsenaturally. - If the loop is terminated by a
break, theelseblock is skipped.
- The
3. Example with a for loop
Letβs say weβre searching for a specific number in a list.
# List of numbers
numbers = [1, 2, 3, 4, 5]
# Number we want to find
target = 6
# Iterate over the list
for num in numbers:
if num == target:
print("Found the target!")
break
else:
print("Target not found in the list.")Explanation:
- The loop checks each number to see if it matches the
target. - If the number is found, the loop breaks, and the
elseclause is skipped. - If the loop finishes without finding the target (i.e., without a
break), theelseblock runs, printing "Target not found in the list."
4. Example with a while loop
# Counter
i = 1
# Loop condition
while i <= 5:
if i == 3:
print("Breaking the loop")
break
print(i)
i += 1
else:
print("Loop finished without breaking.")Explanation:
- The loop runs while
iis less than or equal to 5. - If
iequals 3, the loop breaks. - Since the loop is broken before it naturally ends, the
elseblock is skipped.
π Related Topics
- Python FOR Loops β A
forloop in Python is a programming statement that repeats a block of code a fixed number of times. π Learn more - Python while Loops β A
whileloop in python is a control flow statement that repeatedly executes a block of code until a specified condition is met. π Learn more - Python Loop Control Statements β Learn about Break, Continue & Pass. π Learn more
- Match Statement (Python 3.10+) β Introduced in Python 3.10,
match-caseis a modern way to handle data-driven decision-making. π Learn more