35

I saw someone posted the following answer to tell the difference between if x: pass and if x: continue.

>>> a = [0, 1, 2]
>>> for element in a:
...     if not element:
...         pass
...     print(element)
... 
0
1
2
>>> for element in a:
...     if not element:
...         continue
...     print(element)
... 
1
2

What is the result for if not element when a = 0? Why when using continue, 0 is not printed?

telotortium
  • 3,383
  • 2
  • 23
  • 25
Shelly
  • 569
  • 2
  • 5
  • 7

7 Answers7

34

Using continue passes for the next iteration of the for loop
Using pass just does nothing
So when using continue the print won't happen (because the code continued to next iteration)
And when using pass it will just end the if peacefully (doing nothing actually) and do the print as well

Neo
  • 3,534
  • 2
  • 20
  • 32
  • So if I understand this correctly, `continue` is saying "next value in loop please"? And only "continue" if the `if` statement is `False`? – Binx Mar 10 '21 at 19:26
14

'0' not printed because of the condition "if not element:"

If the element is None, False, empty string('') or 0 then , loop will continue with next iteration.

Anoop
  • 1,415
  • 11
  • 20
  • 2
    It isn't limited to `None` and `0`. It will continue if the element is Falsey. – zondo May 09 '16 at 20:40
  • Does "if not 1" mean False? Then why 1 is printed with continue? In addition, if someone writes "if None", it will also continue without printing? Thanks. – Shelly May 15 '16 at 00:19
  • **1)** Yes. `if not 1` means `False`. In this case condition will fail and execution will continue with `print` statement. This is the reason for printing 1 in your code. `If not ` will be `True` only if the value of ` ` is any falsey values like None, False, empty string('') or 0. **2)** `if None` means always `False` – Anoop May 15 '16 at 18:48
8
if not element:

In both examples, this will only match the 0.

pass

This does nothing. So the next command, print element, will be executed.

continue

This tells Python to stop this for loop cycle and skip to the next cycle of the loop. So print element will never be reached. Instead, the for loop will take the next value, 1 and start from the top.

C14L
  • 12,153
  • 4
  • 39
  • 52
6

There is a fundamental difference between pass and continue in Python. pass simply does nothing, while continue jumps to the next iteration of the for loop. The statement if not 0 always evaluates to True, so both pass and continue statements will be executed. pass will do nothing and print the value, while continue will skip to the next iteration ignoring the print statement written below.

Vedang Mehta
  • 2,214
  • 9
  • 22
5

From: https://docs.python.org/2/tutorial/controlflow.html#pass-statements

The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action.

In your code snippet above if not element will evaluate to true when element = 0. In python 0 is same as boolean false. In the first loop pass does nothing, so it prints all three elements. In the second loop, continue will stop the execution of the rest of loop for that iteration. so the print statement never executes. so it only prints 1 and 2.

WreckeR
  • 409
  • 3
  • 12
1

continue is a control-flow statement used to escape the innermost body of iteration. When your code hits

if not element

the interpreter skips for all values of element that don't validate totrue. 0 is one such value and it skips to the next iteration of the loop when it does not encounter the continue statement and therefore goes on to print the value of element 1 and thereafter 2

In contrast, the pass statement does nothing but skip and return to the next line of code to execute.

Spade
  • 2,220
  • 1
  • 19
  • 29
0
1 a = [0, 1, 2]
2 for element in a:
3     if not element:
4         continue
5.    print(element)

in the first loop, the value of element is 0, and (0 means False), if not False means True. That's why the condition is met, and we go into the if condition's indentation and read continue. When continue is executed, and it sends us to the next loop. In other words, first loop is terminated that's why print(element) is not executed. It results in not printing the value 0.

In the case of pass. It does nothing, when the interpreter reads the statement pass, it moves to the next line which is print(element) and it results in printing the value 0.

Boburbek
  • 21
  • 2