Skip to content

Module 6 · Loops and Iteration — topic 8 of 12

continue

continue skips the rest of the current iteration and moves to the next one. Learn how it simplifies filtering inside loops, its effect in while loops, and how to avoid skipping the update step.

Beginner 10 min read·Lesson 77 of 306 in Python Programming

Learning objectives

  • Skip items with continue
  • Use continue to reduce nesting
  • Avoid infinite loops caused by continue in while

Definition

continue skips the rest of the current iteration and moves to the next one. Learn how it simplifies filtering inside loops, its effect in while loops, and how to avoid skipping the update step.

Introduction

continue ends the current iteration early and jumps straight to the next one. In a for loop that means the next item; in a while loop it means checking the condition again.

Why it matters

Data often contains items you want to ignore: blank lines, invalid records, comments. continue lets you skip them at the top of the loop body and keep the main processing code unindented and readable.

Detailed explanation

Statements after continue in the same iteration are not executed.

In a for loop, continue moves to the next item automatically.

In a while loop, continue jumps back to the condition. Make sure the loop variable has already been updated, or the same value will be processed forever.

continue is like a guard clause for loop bodies: if not wanted: continue, then the normal processing follows.

Many continue-based filters can also be written as comprehensions or with an if around the body; choose whichever reads most clearly.

continue only affects the innermost loop. In nested loops it moves the inner loop on to its next item; the outer loop is unaffected.

When data is skipped, it is good practice to count or log what was skipped. A silent skip can hide bad input that someone should fix, while a count such as “3 rows rejected” makes the problem visible.

Syntax

Text
for item in items:
if should_skip(item):
continue
process(item)
  • continue is usually inside an if.
  • The rest of the body is skipped for that item.
  • The loop carries on with the next item.

Code examples

Skipping odd numbers

Python
for n in range(1, 8):
if n % 2:
continue
print(n)
Output
2
4
6

Ignoring blank and comment lines

Python
lines = ["name=Asha", "", "# comment", "city=Pune"]
for line in lines:
if not line or line.startswith("#"):
continue
key, value = line.split("=")
print(key, "->", value)
Output
name -> Asha
city -> Pune

More examples

Cleaning sales data

Python
records = ["120", "abc", "-5", "300", "", "75"]
total = 0
rejected = 0
for raw in records:
raw = raw.strip()
if not raw.lstrip("-").isdigit():
rejected += 1
continue
amount = int(raw)
if amount < 0:
rejected += 1
continue
total += amount
print("Total:", total, "Rejected:", rejected)
i = 0
while i < 5:
i += 1
if i == 3:
continue
print("processed", i)
Output
Total: 495 Rejected: 3
processed 1
processed 2
processed 4
processed 5

Step-by-step explanation

  • Non-numeric text (abc and the empty string) is counted as rejected and skipped.
  • Negative amounts are rejected by a second continue.
  • Only valid amounts reach total += amount: 120 + 300 + 75 = 495.
  • In the while loop, i is increased before the continue, so skipping 3 can’t cause an infinite loop.

Expected output

Running “Cleaning sales data” prints exactly:

TextOutput
Total: 495 Rejected: 3
processed 1
processed 2
processed 4
processed 5

Practical applications

  • Log analysers skip lines that don’t match the pattern they need.
  • CSV importers skip header or malformed rows.
  • Email filters skip messages already processed.

Best practices

  • Put skip conditions at the top of the loop body.
  • Update while-loop variables before any continue.
  • Count or log skipped items when processing data.

Common mistakes

continue before the update in while

Avoid

i = 0
while i < 5:
if i == 2:
continue
i += 1

Prefer

i = 0
while i < 5:
i += 1
if i == 2:
continue

If the update comes after continue, i stays 2 forever.

Using continue where break was meant

Avoid

if found:
continue

Prefer

if found:
break

continue only skips one iteration; break stops the loop.

Key takeaways

  • continue skips the rest of the current iteration.
  • In while loops, update the variable before continuing.
  • Use it to filter items and reduce nesting.

Try it yourself

  1. 1
    Print the numbers 1–20 except multiples of 4, using continue.
  2. 2
    Sum a list of strings, skipping any that aren’t digits.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

1 question test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat is printed?
Practise this topic