Please enable JavaScript to use CodeHS

Break and Continue in Python

Learn how to use the break and continue statements in your Python programs.

By Rachel Devaney

Python has some nifty tools we can use to tweak the behaviors of for loops and while loops. In this tutorial, we’ll learn about break and continue.

Break

The break statement enables us to immediately exit a loop without executing any of the remaining code in the loop. Typically, we use break statements with a conditional because we want to exit the loop when a specific condition is met. At a high level, a break statement may look like this:

while condition: 
     # code that executes in the loop
     if condition:
          break # immediately exits the loop
    # code that executes in the loop, but is skipped if the break condition is true
Plain text

Break and While Loops

break statements work well with while loops because they avoid repeated code. Take a look at the following code that asks the user to guess a secret number between 1 and 10. This program works, however lines 2 and 6 are the same because we need to get the user’s guess before entering the while loop.

secret_num = 6
guess = int(input("Guess a number, 1-10: "))

while guess != secret_num:
    print("Wrong. Try again.")
    guess = int(input("Guess a number, 1-10: "))

print("You win!")
Plain text

The program below completes the same task but uses a break statement. Now, instead of repeating the user input command before and inside of the while loop, we just use it once inside the loop.

secret_num = 6

while True:
    guess = int(input("Guess a number, 1-10: "))
    if guess == secret_num:
        print("You win!")
        break
    print("Wrong. Try again.")
Plain text

To do this, we use the condition while True for the loop and move the condition to exit the loop, if guess == secret_num, to the break statement. While using while True could result in an infinite loop, we avoid this by using a condition with the break statement.

Run the example below to see the break statement in action. What happens when you move the print("You win!") to after the break statement? Why is this the case?


Break and For Loops
You can also use the break statement in for loops. Take a look at the example for loop below.

  • Try guessing what will happen before you run it!
  • Try changing the condition for the break statement.
  • What happens if you move the print(i) command to before the if statement?

Continue

While the break statement immediately exits the loop, the continue statement immediately returns to the condition of the loop. This enables us to skip specific iterations of the loop without exiting the entire loop. Take a look at the example program below. It’s exactly the same as the for loop above, except that there is a continue statement where the break statement was.

  • Try guessing what will happen before you run it.
  • Try changing the condition of the continue statement.
  • What happens if you move the print(i) command to before the if statement?

Your Turn!

Try your hand at break and continue statements in the code editor below. Here are some practice ideas:

  • Write a program that prints out the numbers 1-20 but skips the unlucky number 13.
  • Write a program that finds the product of a series of numbers that the user enters and prints the result to the screen. You should continue to ask the user for a number until the user enters -1. Multiply each number the user enters and print it to the screen. Hint: You will need to store the product in a variable, and you can use the int(input(prompt)) command to get a number from the user.
  • Extend your product program! Continue asking the user for a number until the user enters -1 or the product is greater than 200.