Skip Ahead in Loops With Python's Continue Keyword

Skip Ahead in Loops With Python's Continue Keyword

by Jon Fincher 0 Comments basics python

Python’s continue keyword functions as a statement that controls the flow of a loop. It allows you to skip code in a loop for the current iteration and jump immediately to the next one. It’s used exclusively in for and while loops, letting you control the flow of execution, bypass specific conditions, and continue processing in a structured and predictable way.

By the end of this tutorial, you’ll understand that:

  • Executing continue doesn’t affect the else clause of a loop.
  • Using continue incorrectly may result in skipping necessary code.
  • You can’t use continue in a function or class that’s nested in a loop.
  • On a bytecode level, continue executes the same instructions as reaching the end of a loop.

Armed with this knowledge, you’ll be able to confidently write loops using continue and expand your skills as a Python programmer.

Take the Quiz: Test your knowledge with our interactive “Skip Ahead in Loops With Python's Continue Keyword” quiz. You’ll receive a score upon completion to help you track your learning progress:


Interactive Quiz

Skip Ahead in Loops With Python's Continue Keyword

Test your understanding of Python's continue keyword, which allows you to skip code in a loop for the current iteration and jump immediately to the next one.

Python’s continue Keyword

Loops are control flow statements used to perform operations repeatedly a certain number of times. In a normal loop, the loop body runs from start to finish, with the number of iterations controlled by the type of loop:

  • A for loop runs a specific number of times and is usually used to process a collection of data.
  • A while loop runs as long as a specific condition evaluates to True. When the condition evaluates to False, the loop ends.

In both cases, you may find it useful to stop the execution of the loop body and move to the next iteration. The way to do that is with the continue keyword.

In any loop, continue stops the code currently executing, and jumps immediately back to the top of the loop, skipping to the next iteration.

Understanding Its Behavior in for and while Loops

In a for loop, continue moves the iterator to the next item to be processed. If no other items are available, then the loop ends.

Assume you have the following for loop that computes the sum of all numbers in a list:

Python
total = 0

for number in range(-10, 10):
    total += number

print(total)

This works fine, but what if you want to add only the positive numbers, ignoring all the negative ones? You can modify this loop to add only positive numbers using continue:

Python
total = 0

for number in range(-10, 10):
    if number < 0:
        continue
    total += number

print(total)

In this case, since Python executes continue only when the number is less than zero, it doesn’t add those numbers to total.

You’ve seen how the continue statement works in a for loop—now you’ll see it working similarly in a while loop.

In a while loop, continue transfers control back to the condition at the top of the loop. If that condition is True, then the loop body will run again. If it’s False, then the loop ends.

Consider the following while loop. It leverages Python’s walrus operator to get user input, casts it to an int, and adds the number to a running total. The loop stops when the user enters 0:

Python sum_whole_numbers.py
print("Enter one whole number per input.")
print("Type 0 to stop and display their sum:")

total = 0

while (user_int := int(input("+ "))) != 0:
    total += user_int

print(f"{total=}")

Again, you only want to add the positive numbers that your users enter, so you modify the loop using continue:

Python sum_whole_numbers.py
print("Enter one whole number per input.")
print("Type 0 to stop and display their sum:")

total = 0

while (user_int := int(input("+ "))) != 0:
    if user_int < 0:
        continue
    total += user_int

print(f"{total=}")

You can copy the code and try it out. When you run the script, Python keeps prompting you for input until you enter 0:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Article

Already a member? Sign-In

Locked learning resources

The full article is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Article

Already a member? Sign-In

About Jon Fincher

Jon taught Python and Java in two high schools in Washington State. Previously, he was a Program Manager at Microsoft.

» More about Jon

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

What Do You Think?

What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.

Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students. Get tips for asking good questions and get answers to common questions in our support portal.


Looking for a real-time conversation? Visit the Real Python Community Chat or join the next “Office Hours” Live Q&A Session. Happy Pythoning!

Become a Member to join the conversation.

Keep Learning

Related Topics: basics python