8

My test shows that both pass and continue can be used equivalently to construct a empty for-loop for test purpose. Are there any difference between them?

user3054997
  • 125
  • 3
  • 10

6 Answers6

9

The pass keyword is a "no-operation" keyword. It does exactly nothing. It's often used as a placeholder for code which will be added later:

if response == "yes":
    pass  # add "yes" code later.

The continue keyword, on the other hand, is used to restart a loop at the control point, such as with:

for i in range(10):
    if i % 2 == 0:
        continue
    print(i)

That loop will only output the odd numbers since continue returns to the loop control statement (for) for iterations where i is even.

Contrast that with the exact same code, but using pass in place of continue:

for i in range(10):
    if i % 2 == 0:
        pass
    print(i)

That loop prints all the numbers in the range, since pass does not return to the loop control statement for even (or any) values of i. It simply drops through to the print statement.

In terms of an empty for loop, you're correct that they're functionally identical. You can use either of:

for i in range(10):
    pass
for i in range(10):
    continue
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 1
    so we could use comment instead of pass right if it is placeholder – The6thSense Jun 19 '15 at 12:22
  • 2
    @The6thSense no, if you have an empty `for` loop with only a comment within it, Python will be confused because it looks for code inside a loop. That's why you need to use `pass` keyword even though it does nothing. Try running this and see the error: ```for i in range(10): # some comment``` (you will see `IndentationError: expected an indented block`). Then try replacing the comment with `pass` and it won't throw an error anymore – nickang Aug 02 '17 at 08:15
  • @nickang thanks for the info mate – The6thSense Aug 03 '17 at 06:40
3

pass does nothing (no operation), while continue make control flow to continue to next cycle of the loop.

falsetru
  • 357,413
  • 63
  • 732
  • 636
3

If the loop contains only a single statement, a pass or continue won't make any difference. But if there are multiple statements, then it matters:

for item in my_list:
    pass
    print(item) #This will be executed

for item in my_list:
    continue
    print(item) #won't be executed

Basically, the pass statement do nothing, while the continue statement will restart the loop.

But in your case:

for item in my_list:
    pass
    #Since there's nothing after pass, the loop is finished.
for item in my_list:
    continue
    #You're restarting the loop

There difference is not very visible.

Hope this helps!

aIKid
  • 26,968
  • 4
  • 39
  • 65
2

continue means "skip to the end of the loop body". If it's a while loop, the loop continues on to the loop test; if it's a for loop, the loop continues on to the next element of whatever it's iterating over.

pass does absolutely nothing. It exists because you have to have something in the body of an empty block statement, and pass is more readable than executing 1 or None as a statement for that purpose.

user2357112
  • 260,549
  • 28
  • 431
  • 505
1

This will lead to an infinite loop if you use continue:

i = 0
while i<1:
   continue  
   i = i + 1
   print i

because continue only goes to the next iteration. But pass will work for this case.

lennon310
  • 12,503
  • 11
  • 43
  • 61
1

pass and continue both work, but may create infinite loops.

For example, the following code will create infinite loop.

i = 0
while i<100:
 continue # replacing continue with pass still creates an infinite loop.

If you want to avoid this (perhaps you intend to modify i withe loop, but you haven't written the code yet), use break.

nick818
  • 41
  • 4