In Python, the Python Break statement applies only to the loop in which it is written that is, the innermost loop. Unlike languages such as Java or PHP, Python does not support labeled continue statements that allow you to skip directly to the next iteration of an outer loop.
To simulate a “continue outer loop” behavior from inside an inner loop, you can use one of the following common techniques:
1. Using a Flag Variable (Recommended for Clarity)
You can set a boolean flag inside the inner loop and evaluate it in the outer loop to decide whether to continue.
for i in range(3):
skip_outer = False
for j in range(3):
if i == 1 and j == 1:
skip_outer = True
break # Exit the inner loop
if skip_outer:
continue # Skips the remaining code in the outer loop for this iteration
print(f"Processed i={i}")
Pros:
- Very readable
- Clearly communicates intent
2. Refactoring Logic into a Function
By placing the nested loops inside a function, you can use return to exit the current outer-loop logic cleanly.
def process_data(i):
for j in range(3):
if i == 1 and j == 1:
return # Ends this function call, mimicking a "continue" on the outer loop
print(f"Processed i={i}")
for i in range(3):
process_data(i)
Pros:
- Clean and structured
- Ideal for complex logic
- Improves modularity and testability
3. Using for...else with continue
In Python, the else block of a loop executes only if the loop completes without encountering a break.
for i in range(3):
for j in range(3):
if i == 1 and j == 1:
break # Triggers break, so the else block is skipped
else:
# Executes only if the inner loop finishes normally (no break)
print(f"Finished inner loop for i={i}")
continue # Proceed to the next iteration of the outer loop
# Executes only if the inner loop was interrupted by break
print(f"Special logic for break at i={i}")
Pros:
- No additional variables required
- Leverages built-in language behavior
Cons:
- Can be unintuitive for beginners
elsebehavior may feel counter-intuitive at first
4. Using Exceptions (Not Recommended for Routine Use)
You can define and raise a custom exception to escape directly to the outer loop.
class ContinueOuter(Exception):
pass
for i in range(3):
try:
for j in range(3):
if i == 1 and j == 1:
raise ContinueOuter()
print(f"Processed i={i}")
except ContinueOuter:
continue
Cons:
- Slower than normal control flow
- Generally considered an anti-pattern for standard looping logic
Understanding Python Loops
Before diving into break, continue, and pass, let’s briefly review Python loops. Loops in Python allow us to execute a block of code multiple times. The two primary types of loops are:
- For Loop – Iterates over a sequence (list, tuple, dictionary, set, or string).
- While Loop – Repeats as long as a given condition remains true.
However, sometimes we need to alter the default execution of these loops. That’s where the Python Break, continue, and pass statements come into play.
Using loops are foundational concepts in Python and virtually every other programming language. When learning the Python Online Course Certification, understanding how loops work is essential for writing efficient and readable code. In Python, there are two primary types of loops: the for loop and the while loop.
A for loop is used to iterate over elements in an iterable such as a string, list, tuple, set, or other sequence, processing each item one by one. In contrast, a while loop repeatedly executes a block of code as long as a specified condition remains true, making it useful for tasks that depend on dynamic conditions rather than a fixed sequence.
But when working with loops, you can change how the iteration is done using the python break, continue and pass statement. In this tutorial, we will discuss how to use these statements to alter the behavior of both for loops and while loops in Python. By the end of this tutorial, you will learn:
- Using the break Statement in Python Break
- Using the break statement in For Loops
- Using the Break Statement on While Loops.
- Using the break Statement with Nested Loops.
- The continue statement in Python Break
- Using the continue Statement in while loops.
- Using the continue statement in nested loops.
- Using the pass statement.
Without further ado, let’s jump into it.
Using the break Statement in Python
The Python Break statement is used to terminate a loop abruptly, based on another condition. When a Python Break statement is used in a nested loop, the inner loop does not run anytime the specific condition is met. It terminates the inner loop and goes on to the outer loop. Let’s see how the break statement works for both for loops, while loops and nested for loops.
Using the break statement in For Loops
Let’s say we have a list of names and we wish to stop the loop when a particular name is found, the Python Break statement can be used for. Refer to the code example shown below.
names = ['Femi', 'Ken', 'David', 'Mah', 'Iliana', 'Hannah', 'Ahmed']
#loop over the list and terminates if the name 'David' is found.
for name in names:
print (name)
#set the condition for the break statement
if name == 'David':
print('David Found. Loop terminates here')
breakOutput: Femi Ken David David Found. Loop terminates here
As seen, the names after David were not printed. This is because the Python Break statement stops the iteration once the name, David, is found.
Now, let’s see how to do the same task with while loops.
Using the Break Statement on While Loops.
The code below shows how to perform the earlier task using while loops.
names = ['Femi', 'Ken', 'David', 'Mah', 'Iliana', 'Hannah', 'Ahmed']
i = 0
#loop over the list and terminates if the name 'David' is found.
while True:
print(names[i])
i += 1
#set the condition for the break statement
if names[i] == 'David':
print('David Found. Loop terminates here')
breakOutput: Femi Ken David Found. Loop terminates here
Note that when looping with the condition while True, it is an infinite loop. When using while loops in such a manner, it is critical to have some specified that would be required to terminate the loop. Else, the code will continue to run until you run out of memory or hit an error.
Using the break Statement with Nested Loops.
A nested loop in Python is a loop inside another loop. When you use the Python Break statement inside the inner loop of a nested loop, the Python code terminates that loop and jumps to the outer loop. The outer loop keeps on running but terminates the inner loop whenever the condition is met. Let’s see an example.
list_1 = [1, 2, 3]
list_2 = ['a', 'b', 'c', 'd']
for x in list_1:
for y in list_2:
if y == 'c':
break
print(f"Outer loop: {x}, Inner loop: {y}")Output: Outer loop: 1, Inner loop: a Outer loop: 1, Inner loop: b Outer loop: 2, Inner loop: a Outer loop: 2, Inner loop: b Outer loop: 3, Inner loop: a Outer loop: 3, Inner loop: b Outer loop: 4, Inner loop: a Outer loop: 4, Inner loop: b
Notice that the inner loop stops a ‘b’ without printing ‘c’ and ‘d’. The outer loop however prints all its elements. This is how the Python Break statement works in nested loops. Now, we move on to the continue statement
The continue statement in Python
The continue statement is used to skip a particular iteration if a condition is met. Notice the difference between a continue statement and a break statement. While the Python Break statement stops the loop, the continue statement only skips that iteration and moves on to the next element. Let’s see how to use the continue statement in Python.
names = ['Femi', 'Ken', 'David', 'Mah', 'Iliana', 'Hannah', 'Ahmed'] #loop over the list and skip the name 'David'. for name in names: #set the condition for the continue statement if name == 'David': continue print(name)
Output: Femi Ken Mah Iliana Hannah Ahmed
You’d realize that the name ‘David’ was not printed. The same thing goes for a while loop.
Using the continue Statement in while loops.
The continue statement can be used in while loops as well. See an example below.
number = 1 while number <= 7: if number == 5: number += 1 continue print(number) number += 1
Output: 1 2 3 4 6 7 Notice that the number 5 was not printed.
Using the continue statement in nested loops.
We can apply the same principle in nested loops. Again, it skips the iteration for that loop and moves on with the next iteration.
list_1 = [1, 2, 3]
list_2 = ['a', 'b', 'c', 'd']
for x in list_1:
for y in list_2:
if y == 'c':
continue
print(f"Outer loop: {x}, Inner loop: {y}")Output: Outer loop: 1, Inner loop: a Outer loop: 1, Inner loop: b Outer loop: 1, Inner loop: d Outer loop: 2, Inner loop: a Outer loop: 2, Inner loop: b Outer loop: 2, Inner loop: d Outer loop: 3, Inner loop: a Outer loop: 3, Inner loop: b Outer loop: 3, Inner loop: d
Notice that this time, the outer loop, ‘d’ was printed. Only ‘c’ was skipped. This is how the continue statement works for nested loops.
Using the pass statement
A pass statement is simply a null statement. It tells the Python interpreter to do nothing once encountered. It is typically used when creating a class or a function that you do not want to populate with codes yet. Using the pass statement would run the empty class or function without an error.
The pass statement can as well be used in loops. As mentioned, it does nothing to a loop. But again, it is useful when you wish to run an empty loop without errors. See the code below.
for x in range(10): pass
Without the pass statement, the code would throw an error. However, the pass statement was used to create the loop without doing anything.
Comparison Table: break vs continue vs pass
| Statement | Functionality |
|---|---|
break | Stops the loop entirely. |
continue | Skips the current iteration and moves to the next. |
pass | Does nothing; acts as a placeholder. |
Key Takeaways
- The
Python Breakstatement Terminates a loop when a condition is met. - The
continuestatement skips the current iteration and moves to the next one. - The
passstatement acts as a placeholder and does nothing. - These statements are essential for writing clean, efficient, and readable Python code.
Conclusion
Mastering Python Break, continue, and pass statements in loops enhances your ability to control program flow effectively. If you’re looking to gain hands-on experience, H2K Infosys offers Python Programming Training Course with expert-led training. Enroll today and take your Python skills to the next level!

























