Python Break, Continue and Pass Statements in Loops

Python Break, Continue and Pass Statements in Loops

Table of Contents

Introduction

Python Break is a powerful and versatile programming language widely used in web development, data science, automation, and more. One of the key aspects of Python is its efficient control flow mechanisms in loops, allowing developers to optimize their code for better performance. Three essential statements that influence loop execution are break, continue, and pass.

Whether you are a beginner looking to Learn Python Online or preparing for a Python programming language certification, understanding these loop control statements is crucial. In this blog, we will cover their functionality with in-depth explanations and real-world examples.

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 Break and virtually every other Python Programming language. In Python, the two kinds of loops are the for loop and the while loop. A for loop is used for parsing the elements in an iterator such as a string, list, tuple, set, etc one after the other. In contrast, the while loop is used to repeat a particular task if a condition is satisfied. 

IT Courses in USA

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')
        break
Output:
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')
        break
Output:
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

StatementFunctionality
breakStops the loop entirely.
continueSkips the current iteration and moves to the next.
passDoes nothing; acts as a placeholder.

Key Takeaways

  • The Python Break statement Terminates a loop when a condition is met.
  • The continue statement skips the current iteration and moves to the next one.
  • The pass statement 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 an online class for Python with expert-led training. Enroll today and take your Python skills to the next level!

Start your journey with H2K Infosys – the best way to learn Python online and achieve your career goals!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share this article
Enroll IT Courses

Enroll Free demo class
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.