Sign In
Sign In

Break, Continue, and Pass Statements in Python Loops

Break, Continue, and Pass Statements in Python Loops
Hostman Team
Technical writer
Python
11.10.2024
Reading time: 4 min

When working with while and for loops, there are times when you need to forcefully exit the loop, skip part of the code, or ignore specific conditions. Python uses the break, continue, and pass statements to handle these cases. Let’s explore how these statements work through examples.

Break Statement

The break statement in Python is used to exit a block of code prematurely. Here’s a simple example:

for j in 'applefishorange':
    if j == 'f':
        break
    print(j)

This will produce the following output:

a
p
p
l
e

As soon as the program encounters the letter f in the sequence, the loop breaks in Python because of the break statement. Now let’s see how it works in a while loop:

x = 0
while x < 5:
    print(x)
    x += 0.5
print('Exit')

The output will look like this (truncated for brevity):

0
0.5
…
4.0
4.5
Exit

Once the condition is no longer met (when x becomes equal to 5), the Python program exits the loop. Now, let’s rewrite the code using the break statement:

x = 0
while True:
    print(x)
    if x >= 4.5:
        break
    x += 0.5
print('Exit')

The result is the same:

0
0.5
…
4.0
4.5
Exit

We assigned the value 0 to x and set the condition: as long as x is True, continue printing it. The code is slightly longer, but using break is justified in situations with complex conditions or to safeguard against infinite loops. Remove two lines from the code above:

x = 0
while True:
    print(x)
    x += 0.5
print('Exit')

And you'll get an endless output:

0
0.5
…
100
100.5
…
1000000
1000000.5
…

And the word Exit will never be printed because the loop will never end. Therefore, using break when working with number sequences helps prevent your program from getting stuck in an infinite loop.

Using Break with Else

Sometimes, you need to check if the loop completed successfully or was interrupted by a break statement in Python. For this, you can use the else clause. Let’s write a program that checks a word for forbidden characters:

word = input('Enter a word: ')
for i in word:
    if i == 'z':
        print('Loop was interrupted, the letter "z" was found')
        break
else:
    print('Loop completed successfully, no forbidden letters found')
print('Check completed')

If the user enters "Hello!", the output will be:

Loop completed successfully, no forbidden letters found
Check completed

But if the input contains the letter "z", the output will be:

Loop was interrupted, the letter "z" was found
Check completed

Explanation: The input function accepts user input (the prompt "Enter a word:" is for the user; the program would work fine with word = input() alone) and assigns it to the variable word. The for loop then iterates over each element (in this case, each letter) and checks it with the condition in the if statement.

Continue Statement

While break interrupts the loop, the continue statement in Python is more flexible — it skips certain elements in the sequence without ending the loop. Let’s write a program that "doesn’t like" the letter "z":

word = input('Enter a word: ')
for i in word:
    if i == 'z':
        continue
    print(i)

If you enter "zebra", the output will be:

e
b
r
a

This happens because we set the condition where any element with the value "z" is not printed. But the Python continue statement allows the loop to finish, printing all "allowed" elements. However, there is a small issue with the code: if the user enters "Zebra" (with an uppercase Z), the program will print the entire word because we didn’t account for the letter case:

Z
e
b
r
a

The most obvious solution here is to add the uppercase letter in the if block like this:

word = input('Enter a word: ')
for i in word:
    if i == 'z' or i == 'Z':
        continue
    print(i)

Pass Statement

The pass statement in Python allows the loop to continue regardless of any conditions. It is rarely seen in final code but is useful during development as a placeholder for code that hasn’t been written yet. For example, let’s say you need to remember to add a condition for the letter "z", but you haven't written that block yet. Here, the pass placeholder keeps the program running smoothly:

word = input('Enter a word: ')
for i in word:
    if i == 'z':
        pass
else:
    print('Loop completed, no forbidden letters found')
print('Check completed')

Now the program will run, and pass will act as a marker to remind you to add the condition later.

That’s all! We hope that break, continue, and pass in Python will soon become your reliable tools for developing interesting applications. Good luck!

Python
11.10.2024
Reading time: 4 min

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start
Email us