TNS
VOXPOP
Do You Resent AI?
If you’re a developer, do you resent generative AI’s ability to write code?
Yes, because I spent a lot of time learning how to code.
0%
Yes, because I fear that employers will replace me and/or my peers with it.
0%
Yes, because too much investment is going to AI at the expense of other needs.
0%
No, because it makes too many programming mistakes.
0%
No, because it can’t replace what I do.
0%
No, because it is a tool that will help me be more productive.
0%
No, I am a highly evolved being and resent nothing.
0%
I don’t think much about AI.
0%
Python / Software Development

What Are Loop Controls in Python and How Do You Use Them?

This tutorial will teach you how to use the break, continue, and pass loop controls in Python.
Apr 26th, 2024 4:00am by
Featued image for: What Are Loop Controls in Python and How Do You Use Them?
Feature image via Unsplash.

Loops are an integral part of your Python code, without which you’d struggle to do anything effectively (or without having to use far too much code for the purpose). With loops, you can create automated tasks that repeat actions until a particular condition is met. You can use for loops to iterate over a sequence of items and while loops to continue executing statements as long as a condition is true.

But you might find there are times when you need to exit the loop, skip an iteration, or ignore a condition. For example, you might create a loop that executes a statement until a condition is met but you need to break out of the loop if a different condition occurs.

Here’s a simple example:

You create a for loop to print out “Hello, New Stack!” but you want to break out of the loop if it discovers the S character. That’s very simple and doesn’t really have much in the way of real-world application, but it makes for a good demonstration.

Let’s create that bit of code.

We’ll start by defining our text like this:


Then we create a for loop that iterates through the string and includes a break statement to catch the S character. That loop looks like this:


The entire code block looks like this:


If you run the above code, the output would look like this:

H
e
l
l
o
,

N
e

S
For Loop Broken

As you can see, as soon as the for loop hits S, it breaks the loop. We can also do this with a while loop, like so:


Run the above code and you’ll get the same output you did with the for loop.

Another form of loop control is the continue statement which is used to skip the execution of the statement block and returns control to the beginning of the loop to start the next iteration. In other words, the remaining statements in the loop will be skipped for the current iteration and the loop will begin the next iteration.

Remember, back with our break statement, when the loop reached the letter S, it broke such that everything past that character was ignored. With the continue statement, the loop reaches the S and immediately returns and iterates again, so it can print out the remaining characters. The code for this might look something like this:


If you were to run the above, the output would be:

Current Letter = N
Current Letter = e
Current Letter = w
Current Letter =
Current Letter = t
Current Letter = a
Current Letter = c
Current Letter = k

As you can see, our loop skips S but continues to iterate so the entire string (minus the S) is printed.

We can do this with a while loop as well. This time it will count backwards from 10 to 0 but skip 7. That code looks like this:


Run the above and you get:

Current Value : 10
Current Value : 9
Current Value : 8
Current Value : 6
Current Value : 5
Current Value : 4
Current Value : 3
Current Value : 2
Current Value : 1
Current Value : 0

Another loop control is the pass statement, which acts as a sort of a null operation. When using a pass loop control, no code is executed. This can come in handy when you want to write empty loops (which can be used as a placeholder for code you plan on writing later or when you want to consume an iterator).

Let’s create a block of code that prints out New Stack but also includes a for loop and a function definition, both of which do nothing and use the pass statement. That code might look something like this:


If you were to run the above code, the output would be:

N
e
w

Pass Statement Used
S
t
a
c
k

As you can see, both our initial for loop and our function are both skipped because they include the pass statement.

And that’s how you use the break, continue, and pass loop controls in Python. With the help of these three statements, you can more effectively and efficiently use loops. As you gain more experience with the language, you’ll come to appreciate these three loop control statements.

Group Created with Sketch.
TNS owner Insight Partners is an investor in: Statement.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.