Nearby lessons

50 of 159

Python - Continue Statement

📌 What You Will Learn
  • Define the continue statement and how it skips the current iteration
  • Write a for loop that skips a specific number using continue
  • Print only odd numbers by skipping even values
  • Print only even numbers by skipping odd values
  • Apply continue for skipping invalid records and filtering data

continue Statement

The continue statement skips the current iteration and moves to the next iteration.

The loop does not stop.

Syntax

🐍Code Cell
1for variable in sequence:
2 if condition:
3 continue
Output
No output captured.

Example 1 - continue Statement

🐍Code Cell
1for i in range(10):
2 if i == 5:
3 continue
4 
5 print(i)
Output
0
1
2
3
4
6
7
8
9

Explanation

When i becomes 5, the continue statement skips that iteration.

The remaining iterations continue normally.

Example 2 - Print Odd Numbers

🐍Code Cell
1for i in range(10):
2 
3 if i % 2 == 0:
4 continue
5 
6 print(i)
Output
1
3
5
7
9

Example 3 - Print Even Numbers

🐍Code Cell
1for i in range(10):
2 
3 if i % 2 != 0:
4 continue
5 
6 print(i)
Output
0
2
4
6
8

Real World Usage of continue

The continue statement is useful for:

  • Skipping invalid records
  • Ignoring unwanted values
  • Filtering data
📝 Key Takeaways
  • The continue statement skips the current iteration and moves to the next one
  • The loop does not stop when continue is used
  • continue is useful for filtering unwanted values and skipping invalid records
  • The remaining iterations continue normally after continue
  • break stops the loop while continue only skips one iteration

🧠 Test Your Knowledge

2 Questions
Progress: 0 / 2