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 theelse
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.
Get Your Code: Click here to download the free sample code that shows you how to skip ahead in loops with Python’s continue keyword .
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 KeywordTest 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 toTrue
. When the condition evaluates toFalse
, 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:
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
:
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
:
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
:
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
: