Loops in Python 3 featured image

Loops in Python 3: Using Break, Continue, and Pass Statements

Introduction

There are two types of loops that you can employ in Python 3. You have for loops and while loops. You can use them for repeat tasks. As a result, the repetitive tasks will happen automatically, making the process more efficient. Unfortunately, your loops can encounter some problems. At times, your program may run into an issue where you need it to skip a part of the loop or exit it entirely. Or maybe you need it to ignore the external factor that is influencing the program. If this is something you want to add to your program, you need to use the break, continue, and pass statements.

In this guide, we will discuss how you can use the break, continue, and pass statements when working with loops in Python 3.

How to Use Break Statement

The break statement lets you exit the loop in the presence of an external influence. You will need to place this statement in the code of your loop statement. We typically use it with a conditional if statement. To help you understand, let’s take the example of the following loop. Here, we are using a break statement in a for loop:

As you can see, we initialize the variable number at 0. We then put in a for statement to make the loop. The condition is that the number is less than 10. After that, we added an if statement. It states that if the variable number equals 5, then the loop will be broken. Another factor in the loop code is the print() statement which executes repeatedly with each loop until it breaks. Finally, the last print() statement allows us to be alerted when we exit the loop.

Adding and running the code with this break statement will give you an output like this:

break output

This shows that once the variable number became equivalent to 5, the loop broke. This means the program is out of the loop now.

How to Use Continue Statement

With the continue statement, you can successfully skip only a certain part of the loop. Thus,  when your program encounters a trigger, it will skip a preset part of the loop and will continue to complete the rest of it from the top in a new iteration. Again, you will need to use the if statement. Let’s use the same example as the previous section. Here, instead of a break statement, we will apply a continue statement:

The obvious difference between running break statements and continue statements is that with the latter, we do not exit the loop. Instead, the code continues in the next iteration even when the variable number is equivalent to 5. The output will look something like this:

continue output python

As you can see, the output does not show the value Number is 5. The loop simply continues beyond it. It prints outlines for the numbers 6, 7, 8, 9, and 10 before it exits the loop. This is useful when you want to avoid some conditional code.

How to Use Pass Statement

The pass statement lets you deal with the external trigger without having to disrupt the loop. This means that regardless of the outside factor, the loop will continue printing lines unless it encounters another statement. Similarly to the other two statements, we will add this statement in the loop code after a conditional if statement. Let’s apply this statement to our example:

With the pass statement, we are telling the program to ignore the fact that the variable number is equal to 5. Running this code will give you this output:

pass output

This shows that the trigger did not have any impact whatsoever on the loop. It seems as if there was no conditional statement at all. As such, you can use the pass statement to make minimal classes or even use it as a placeholder on codes that are still in the making.

Finally, you can take a look at our other tutorials that will help you get familiar with what you can do with Python:

Conclusion

In this guide, we helped you understand what each of the break, continue, and pass statements are and how they work. Now you know how you can add them to the block of code of your loop statements in Python 3. They will help you make your for and while loops more effective in your program.

Happy Computing!