How to use Break, Continue, and Pass statements when working with loops in Python

0 Shares
0
0
0
0

Introduction

Using for loops and while loops in Python allows you to repeat tasks automatically and efficiently.

But sometimes, an external factor may affect how your program runs. When this happens, you may want your program to break out of a loop entirely, skip part of a loop before continuing, or ignore that external factor. You can do these actions with the break , continue , and pass statements.

Prerequisites

You must have Python 3 installed and a development environment on your computer or server. Let's say you haven't set up a development environment. In that case, you can refer to the installation and setup guide to set up a local Python development environment on your server appropriate for your operating system (Ubuntu, CentOS, Debian, etc.).

BREAK command

In Python, the break statement allows you to exit a loop when an external condition is triggered. You place the break statement in the block of code below your loop statement, usually after an if conditional statement.

Let's look at an example that uses the break statement in a for loop:

number = 0

for number in range(10):
if number == 5:
break # break here

print('Number is ' + str(number))

print('Out of loop')

The variable number in this small program is initialized to 0. Then, if the variable number is less than 10, it creates a for loop.

In a for loop, an if statement provides a condition that the loop will break if the variable number is equal to the integer 5.

Inside the loop there is also a print() statement that is executed with each iteration of the for loop until the loop breaks, because it is after the break statement.

Let's put a final print() statement outside the for loop so we know when we're out of the loop.

When you run this code, you will get the following output:

Output
Number is 0
Number is 1
Number is 2
Number is 3
Number is 4
Out of loop

This shows that the loop breaks when the integer evaluates to 5, as the program is told to do with the break statement.

The break statement causes a program to exit a loop.

CONTINUE command

The continue statement allows you to skip the part of a loop where an outer condition is triggered, but continue the loop. The current iteration of the loop is interrupted, but the program returns to the top of the loop.

The continue statement is placed in the code block below the loop statement, usually after an if conditional statement.

Using the same for loop program in the Break Statement section above, we will use the continue statement instead of the break statement:

number = 0

for number in range(10):
if number == 5:
continue # continue here

print('Number is ' + str(number))

print('Out of loop')

The difference in using the continue statement instead of the break statement is that our code continues despite the interruption when the variable number evaluates to 5. Let's examine our output:

Output
Number is 0
Number is 1
Number is 2
Number is 3
Number is 4
Number is 6
Number is 7
Number is 8
Number is 9
Out of loop

Here, the number 5 never occurs in the output, but the loop continues after that point to print lines for the numbers 6-10 before exiting the loop.

You can use the continue statement to avoid deeply nested conditional code or to optimize a loop by eliminating items you want to skip.

The continue statement causes a program to skip certain elements that appear in a loop but then continue through the rest of the loop.

PASS command

When an external condition is triggered, the pass statement allows you to handle the condition without affecting the loop in any way. All code continues to read unless a failure or other statement occurs.

Like other statements, the pass statement will be placed in the code block below the loop statement, usually after an if conditional statement.

Using the same code block above, let's replace the break or continue statement with a pass statement:

number = 0

for number in range(10):
if number == 5:
pass # pass here

print('Number is ' + str(number))

print('Out of loop')

After the if conditional statement, the pass statement tells the program to continue executing the loop and ignore that the variable number evaluates to 5 during one of its iterations.

You run the program and get the following output:

Output
Number is 0
Number is 1
Number is 2
Number is 3
Number is 4
Number is 5
Number is 6
Number is 7
Number is 8
Number is 9
Out of loop

By using the pass statement in this program, you will notice that the program executes exactly as it would if the conditional statement were not present in the program. The pass statement tells the program to ignore the condition and continue executing the program as usual.

The pass statement can create minimal classes, or act as a placeholder when working on new code and thinking at the algorithmic level before hammering out the details.

Result

The break, continue, and pass statements in Python allow you to use for and while loops more effectively in your code.

Leave a Reply

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

You May Also Like