11) User Input and String Formatting Lesson

Python Continue and Python Break Statements

8 min to complete · By Martin Breuss

Sometimes, you might want to exit your loop early. Doing so could save execution time. Python offers a few loop keywords that allow you to change the flow of execution within your loops. When you use the right loop keywords, you can even escape from an otherwise infinite loop.

Python has two loop keywords that can change the flow of execution in a loop:

  1. break
  2. continue

In this lesson, you'll learn what these two keywords mean and when to use them.

Python Break Statement

The break keyword ends the execution of the loop. It skips any code that comes after it inside the loop body. The execution continues after the loop body.

Python Break For Loop

The following example code prints all consonants in a word until it encounters the first vocal. Then, the break keyword kicks in and kicks you out of the loop. The code continues to run after the loop body, effectively skipping print(char):

word = "hello"

for char in word:
  if char in "aeiou":
    break
  print(char)

print("done")

Try the code in your text editor. What does it print for the word cradle?

Python break While Loop

You can use the break statement to break out of an infinite loop, for example, when you are waiting for a specific input from a user. Using this keyword, you can write your code in a way that creates an infinite loop that has an exit condition that leads to a break keyword:

# Always make sure there is a condition that terminates a while loop
while True:
  prompt = input('say something: ')
  if prompt == 'quit':
    break
  print(prompt[::-1])
print("bye!! (it's tiuq, btw. ;)")

In the example above, you are creating an infinite loop with the while True: opening statement. This will never be False, so the loop would keep going forever.

However, you also added an exit condition with the break statement:

if prompt == "quit":
    break

The while loop incessantly echoes the reverse string of what the user typed unless they type the word quit. This makes your conditional statement evaluate to True and the code inside the conditional block executes.

The conditional block consists only of the break statement, which is enough to kick your program out of the infinite loop.

Practice the Break Keyword

  • Write the same program without using the break keyword. How do you need to structure it so it still has an exit condition?
  • What's a possible advantage of using the while loop with the break statement over the one without it?

The break keyword gives you additional possibilities for structuring the flow of execution in your loops.

Python continue Statement

The continue keyword jumps back to the beginning of the loop. It skips any code following the keyword in that specific run of the loop. The loop doesn't quit. However, it just starts again from the top with the next round of execution:

word = "hello"

for char in word:
    if char in "aeiou":
        continue
    print(char)

print("done")

If you run this code in your IDE, then you'll see that it prints out every consonant of the word but skips printing the vocals.

Break vs Continue

Learn by Doing

Compare this code snippet to a similar one that uses the break keyword instead. Run them both and investigate their different outputs.

You can also use the continue statement in both the for and the while loops. Each type of loop and each loop keyword dictate a different setup of your code.

Often, you can achieve the same outcome with a couple of different setups. You can mix and match different loops and different loop keywords. You can practice translating between different setups by taking existing loops and re-writing them using a while loop with a continue keyword, etc.

Colorful illustration of a light bulb

Info: Getting more familiar with different ways of writing the same functionality helps you understand that there isn't one "right" way to write code. Instead, it's a creative process where you can often pick what works best for you in a specific situation.

Additional Resources

In a future lesson, you'll use your newfound knowledge about loops and user input to build out a command-line game. That's right! Time to build a game!

Summary: Python Continue and Python Break Statements

  • Python has two keywords to alter the flow of execution in your loops -break** quits the execution of a loop
  • Nothing within the loop body that follows the keyword will be run and your code continues after the loop
  • continue skips the rest of the current iteration of your loop
  • continue execution back to the beginning of the loop, and the next iteration begins