The continue statement is used to skip the remaining code of the current iteration of a loop and move directly to the next iteration.
The continue statement tells Python to skip the remaining statements in the current loop iteration.
for variable in sequence:
if condition:
continue
statement
After continue executes, Python moves to the next iteration.
for i in range(1, 6):
if i == 3:
continue
print(i)
When i is 3, continue skips the print statement for that iteration.
i = 0
while i < 5:
i += 1
if i == 3:
continue
print(i)
for i in range(1, 11):
if i % 2 == 0:
continue
print(i)
The continue statement skips all even numbers.
for i in range(1, 11):
if i % 2 != 0:
continue
print(i)
numbers = [10, 20, 30, 40, 50]
for number in numbers:
if number == 30:
continue
print(number)
text = "Python"
for letter in text:
if letter == "h":
continue
print(letter)
for i in range(1, 11):
if i < 5:
continue
print(i)
i = 0
while i < 10:
i += 1
if i <= 5:
continue
print(i)
for i in range(5):
number = int(input("Enter a number: "))
if number < 0:
continue
print("Positive number:", number)
If the user enters a negative number, the print statement is skipped for that iteration.
for i in range(1, 11):
if i == 7:
continue
print("Number:", i)
When continue is used inside a nested loop, it affects the nearest loop containing it.
for i in range(1, 4):
for j in range(1, 4):
if j == 2:
continue
print(i, j)
number = 5
for i in range(1, 11):
if i == 5:
continue
print(number * i)
The fifth multiplication result, 25, is skipped.
names = ["Amit", "Rahul", "Ravi", "Priya"]
for name in names:
if name == "Ravi":
continue
print("Hello", name)
| Continue | Break |
|---|---|
| Skips the current iteration. | Stops the entire loop. |
| The loop continues. | The loop terminates. |
| Used when a particular value should be skipped. | Used when the loop should stop completely. |
Continue:
for i in range(5):
if i == 2:
continue
print(i)
Pass:
for i in range(5):
if i == 2:
pass
print(i)
The pass statement does nothing, while continue skips the remaining code of the current iteration.
students = {
"Amit": 80,
"Rahul": 45,
"Priya": 90
}
for name, marks in students.items():
if marks < 50:
continue
print(name, marks)
marks = [35, 65, 40, 80, 25, 90]
for mark in marks:
if mark < 40:
continue
print("Pass:", mark)
numbers = [10, -5, 20, -3, 30]
for number in numbers:
if number < 0:
continue
print(number)
When using continue in a while loop, make sure the loop variable is updated before continue. Otherwise, the loop may become infinite.
i = 0
while i < 5:
i += 1
if i == 3:
continue
print(i)
Unlike break, continue does not terminate the loop. Therefore, the loop can still finish normally and its else block can execute.
for i in range(1, 5):
if i == 2:
continue
print(i)
else:
print("Loop completed")
Suppose we have a list of student marks and want to display only passing marks.
marks = [25, 45, 30, 70, 85]
for mark in marks:
if mark < 40:
continue
print("Passed:", mark)
Question: What does the continue statement do in Python?