Difference between continue and pass in Python
Using the continue
statement
A continue
statement is used within a for loop to skip the remainder of the current iteration. Essentially, the continue
statement will simply skip to the next iteration.
A normal for
loop that prints the current index i
:
1for i in range(3):
2 print(i)
10
21
32
The same for loop, with a conditional and a continue
:
1for i in range(3):
2 if i == 1:
3 continue
4 print(i)
10
22
On the second iteration of the for loop (when i = 1
), the continue
statement stopped the current iteration and “continued” to the next.
Using the pass
statement
As implied by the name, the pass
state does nothing.
1for i in range(3):
2 if i == 1:
3 pass
4 print(i)
10
21
32
This is most useful when defining placeholders for future code to satisfy the interpreter.
1def func():
2 pass