How to Stop a For Loop in Python

5/5 - (2 votes)

Python provides three ways to stop a for loop:

  1. The for loop ends naturally when all elements have been iterated over. After that, Python proceeds with the first statement after the loop construct.
  2. The keyword break terminates a loop immediately. The program proceeds with the first statement after the loop construct.
  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.

You can see each of these three methods to terminate a for loop in the following graphic:

How to Stop a For Loop in Python

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! โคต๏ธ

Python Cheat Sheet Keywords

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.