When processing data in a loop, there are times when you want to “skip specific items that meet certain conditions” without stopping the entire process. For example, you might want to ignore image files in a file list or exclude invalid data.
To achieve this control of “interrupting the current process and moving to the next loop (iteration),” Python uses the continue statement.
This article explains the basic usage of the continue statement and how it differs from the break statement, which terminates the loop completely.
Basic Behavior of the continue Statement
When the continue statement is executed, the Python interpreter skips all remaining processing in the current loop and immediately returns to the start of the loop to begin processing the next element.
Syntax:
for variable in iterable:
if condition_to_skip:
continue # Jump to the next loop iteration here
# Normal processing (Executed only if the condition is not met)
Specific Example: File Filtering
As an example, let’s create a program that processes only “text files (.txt)” from a list of filenames and skips other files.
# List of files to process
file_list = ["report_2024.txt", "profile.png", "data.csv", "memo.txt", "app.py"]
print("--- Start processing text files ---")
for filename in file_list:
# Skip if the filename does not end with ".txt"
if not filename.endswith(".txt"):
print(f"[Skip] Non-target file: {filename}")
continue
# The code below runs only for ".txt" files
print(f"Processing... Reading text file: {filename}")
# Write file reading processing here
Output:
--- Start processing text files ---
Processing... Reading text file: report_2024.txt
[Skip] Non-target file: profile.png
[Skip] Non-target file: data.csv
Processing... Reading text file: memo.txt
[Skip] Non-target file: app.py
Explanation:
report_2024.txtdoes not match the skip condition, socontinueis not executed, and “Processing…” is displayed.profile.pngmatches the condition, so after displaying “[Skip]…”,continueis executed.- This causes the code immediately below (“Processing…”) to be ignored, and the loop immediately moves on to process
data.csv.
Difference from the break Statement
continue is often compared with break. Their behaviors are clearly different.
continue: Interrupts processing “only for this time.” The loop itself does not end; it proceeds to the next element. (e.g., “Skip this file and check the next one.”)break: Interrupts “the loop itself.” Processing ends completely even if elements remain. (e.g., “An error occurred, so stop the entire process.”)
Comparison Code:
numbers = [1, 2, 999, 4, 5]
print("--- With continue ---")
for n in numbers:
if n == 999:
continue # Skip only 999
print(n)
print("\n--- With break ---")
for n in numbers:
if n == 999:
break # Stop when 999 is found
print(n)
Output:
--- With continue ---
1
2
4
5
--- With break ---
1
2
In the case of continue, 4 and 5 (which come after 999) are displayed. In the case of break, the loop stops completely at 999.
Summary
- Use the
continuestatement to skip processing only when specific conditions are met inside a loop and move to the next iteration. - It is effective when you want to loop while excluding (filtering) error data or non-target data.
- Its role is different from the
breakstatement, which ends the loop itself, so use them according to your needs.
