Python provides three ways to stop a for loop:
- The for loop ends naturally when all elements have been iterated over. After that, Python proceeds with the first statement after the loop construct.
- The keyword
break
terminates a loop immediately. The program proceeds with the first statement after the loop construct. - The keyword
continue
terminates only the current loop iteration, but not the whole loop. The program proceeds with the first statement in the loop body.
You can see each of these three methods to terminate a for loop in the following graphic:

Let’s dive into each of those three approaches next!
Method 1: Visit All Elements in Iterator
The most natural way to end a Python for
loop is to deplete the iterator defined in the loop expression for <var> in <iterator>
. If the iterator’s next()
method doesn’t return a value anymore, the program proceeds with the next statement after the loop construct. This immediately ends the loop.
Here’s an example that shows how the for loop ends as soon as all elements have been visited in the iterator returned by the range()
function:
s = 'hello world' for c in range(5): print(c, end='') # hello
๐ Recommended Tutorial: Iterators, Iterables, and Itertools
Method 2: Keyword “break”
If the program executes a statement with the keyword break
, the loop terminates immediately. No other statement in the loop body is executed and the program proceeds with the first statement after the loop construct. In most cases, you’d use the keyword break
in an if construct to decide dynamically whether a loop should end, or not.
In the following example, we create a string with 11 characters and enter a for loop that ends prematurely after five iterations — using the keyword break
in an if
condition to accomplish that:
s = 'hello world' for i in range(10): print(s[i], end='') if i == 5: break # hello
As soon as the if
condition evaluates to False
, the break
statement is executed—the loop ends.
๐ Recommended Tutorial: How to End a While Loop?
Method 3: Keyword “continue”
The keyword continue
terminates only the current loop iteration, but not the whole loop. The program proceeds with the first statement in the loop body. The most common use of continue is to avoid the execution of certain parts of the loop body, constrained by a condition checked in an if construct.
Here’s an example:
for i in range(10): if i == 5: break else: continue print('NEVER EXECUTED')
Python iterates over an iterator with 10 elements. However, in each iteration, it either ends the loop using break
or continues with the next iteration using continue
.
However, the remaining loop body that actually does something such as printing 'NEVER EXECUTED'
is, well, never executed.
Python Keywords Cheat Sheet
You can learn about the most important Python keywords in this concise cheat sheet—if you’re like me, you love cheat sheets as well! โคต๏ธ

You can download it here:
Summary
You’ve learned three ways to terminate a while loop.
- Method 1: The for loop terminates automatically after all elements have been visited. You can modify the iterator using the
__next__()
dunder method. - Method 2: The keyword
break
terminates a loop immediately. The program proceeds with the first statement after the loop construct. - Method 3: The keyword
continue
terminates only the current loop iteration, but not the whole loop. The program proceeds with the first statement in the loop body.
Thanks for reading this tutorial—if you want to boost your Python skills further, I’d recommend you check out my free email academy and download the free Python lessons and cheat sheets here:
Join us, it’s fun! ๐
Programmer Humor
โ Question: How did the programmer die in the shower? โ ๏ธ
โ Answer: They read the shampoo bottle instructions:
Lather. Rinse. Repeat.

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.