Introduction
The continue statement allows the program to skip the remaining code inside the current iteration and move directly to the next iteration of the loop. Unlike the break statement, which completely terminates a loop, the continue statement only skips the current iteration while allowing the loop to continue running.
In this tutorial, you will learn what the Python continue statement is, its syntax, examples, real-life applications, common mistakes, interview questions, and best practices.
What is Python Continue Statement?
The continue statement is a loop control statement used to skip the remaining code in the current iteration and proceed directly to the next iteration of the loop.
When Python encounters a continue statement:
- The remaining code inside the loop is skipped.
- The loop immediately moves to the next iteration.
- The loop continues until its condition becomes false.
Example:
for number in range(1, 6):
if number == 3:
continue
print(number)
Output:
2
4
5
Explanation:
- The loop starts from 1.
- When number becomes 3, the continue statement executes.
- Python skips the print() statement for that iteration.
- The loop continues with 4 and 5.
Why Use the Continue Statement?
The continue statement is useful when:
- Certain values should be ignored.
- Invalid data needs to be skipped.
- Specific conditions should not be processed.
- Filtering is required during iteration.
- Processing should continue without terminating the loop.
Using continue makes code cleaner and avoids unnecessary nested conditions.
Syntax
The basic syntax of the continue statement is:
for variable in sequence:
if condition:
continue
# remaining code
Or with a while loop:
while condition:
if condition2:
continue
# remaining code
Syntax Breakdown
- continue must be inside a loop.
- It is usually placed inside an if statement.
- Execution jumps to the next iteration immediately.
Continue Statement with For Loop
The continue statement is frequently used inside for loops.
Example:
for num in range(1, 6):
if num == 4:
continue
print(num)
Output:
2
3
5
The number 4 is skipped.
Continue Statement with While Loop
Example:
count = 0
while count < 5:
count += 1
if count == 3:
continue
print(count)
Output:
2
4
5
When count becomes 3, Python skips the print statement and continues with the next iteration.
Skipping Even Numbers
Example:
for num in range(1, 11):
if num % 2 == 0:
continue
print(num)
Output:
3
5
7
9
Only odd numbers are displayed.
Skipping Odd Numbers
Example:
for num in range(1, 11):
if num % 2 != 0:
continue
print(num)
Output:
4
6
8
10
Only even numbers are displayed.
Continue Statement with Strings
Example:
word = "PYTHON"
for letter in word:
if letter == "H":
continue
print(letter)
Output:
Y
T
O
N
The letter “H” is skipped.
Continue Statement with Lists
Example:
numbers = [10, 20, 30, 40, 50]
for num in numbers:
if num == 30:
continue
print(num)
Output:
20
40
50
The value 30 is ignored.
Continue Statement in Nested Loops
The continue statement only affects the current loop.
Example:
for i in range(1, 4):
for j in range(1, 4):
if j == 2:
continue
print(i, j)
Output:
1 3
2 1
2 3
3 1
3 3
Only the value 2 in the inner loop is skipped.
Difference Between Continue and Break
Many beginners confuse continue with break.
| Feature | Continue | Break |
|---|---|---|
| Purpose | Skip current iteration | Exit loop completely |
| Loop Execution | Continues running | Stops running |
| Next Action | Moves to next iteration | Exits loop |
Continue Example
for num in range(5):
if num == 2:
continue
print(num)
Output:
1
3
4
Break Example
for num in range(5):
if num == 2:
break
print(num)
Output:
1
Continue Statement with Else Block
Example:
for num in range(1, 6):
if num == 3:
continue
print(num)
else:
print("Loop Completed")
Output:
2
4
5
loop completed
Unlike break, continue does not prevent the else block from executing.
Example: Skip Negative Numbers
Example:
numbers = [10, -5, 20, -8, 30]
for num in numbers:
if num < 0:
continue
print(num)
Output:
20
30
Negative numbers are ignored.
Example: Skip Empty Strings
names = ["John", "", "David", "", "Emma"]
for name in names:
if name == "":
continue
print(name)
Output:
David
Emma
Empty values are skipped.
Real-Life Examples:
1. Student Attendance System
students = [
"John",
"David",
"Absent",
"Emma"
]
for student in students:
if student == "Absent":
continue
print("Present:", student)
Output:
Present: David
Present: Emma
Schools may use similar logic while generating attendance reports.
2. E-Commerce Order Processing
orders = [
"Completed",
"Cancelled",
"Completed",
"Pending"
]
for order in orders:
if order == "Cancelled":
continue
print("Processing:", order)
Output:
Processing: Completed
Processing: Pending
Cancelled orders are skipped.
3. Email Validation
emails = [
"user1@example.com",
"",
"user2@example.com"
]
for email in emails:
if email == "":
continue
print("Sending Email:", email)
Output:
Sending Email: user2@example.com
Invalid email addresses are ignored.
4. Exam Result Analysis
marks = [80, -1, 75, 90]
for mark in marks:
if mark < 0:
continue
print("Valid Mark:", mark)
Output:
Valid Mark: 75
Valid Mark: 90
Invalid marks are skipped during processing.
Advantages of Continue Statement
1. Improves Readability
Reduces unnecessary nested if statements.
2. Simplifies Filtering
Easily skips unwanted values.
3. Maintains Loop Execution
The loop continues instead of terminating.
4. Useful for Data Validation
Helps ignore invalid records.
5. Enhances Performance
Avoids processing unnecessary data.
Common Mistakes
1. Using Continue Outside a Loop
Incorrect:
if True:
continue
Output:
Continue can only be used inside loops.
2. Forgetting to Update Variables in While Loops
Incorrect:
count = 0
while count < 5:
if count == 2:
continue
count += 1
This creates an infinite loop.
Correct:
count = 0
while count < 5:
count += 1
if count == 2:
continue
print(count)
3. Confusing Continue with Break
Many beginners expect continue to stop the loop.
continue
Only skips the current iteration.
4. Overusing Continue
Too many continue statements can make code difficult to understand.
5. Placing Important Code After Continue
Incorrect:
if num == 3:
continue
print("Skipped")
The print statement never executes.
Best Practices
1. Use Continue for Filtering
if record_invalid:
continue
2. Keep Conditions Simple
Readable conditions improve code quality.
3. Avoid Excessive Continue Statements
Too many continue statements can reduce readability.
4. Use Meaningful Variable Names
for student in students:
Instead of:
for x in students:
5. Be Careful in While Loops
Always update loop variables before continue executes.
Conclusion
The Python continue statement is a powerful loop control statement that allows developers to skip the current iteration and move directly to the next one. Unlike the break statement, which terminates the loop completely, continue keeps the loop running while ignoring specific values or conditions.
It is especially useful in data filtering, validation, attendance systems, order processing, email handling, and many other real-world applications.
Python Continue Statement – Interview Questions
Q 1: What does the continue statement do?
Q 2: Does continue terminate the loop?
Q 3: Where can continue be used?
Q 4: What is the difference between break and continue?
Q 5: Is continue used inside conditions?
Python Continue Statement – Objective Questions (MCQs)
Q1. What is the purpose of the continue statement in Python?
Q2. What will be the output of the following code?
for i in range(5):
if i == 2:
continue
print(i)
Q3. The continue statement can be used inside:
Q4. What will the following code print?
i = 0
while i < 5:
i += 1
if i == 3:
continue
print(i)
Q5. What happens after the continue statement is executed inside a loop?