if pass and if continue in python
Asked Answered
D

7

41

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?

Deponent answered 9/5, 2016 at 20:17 Comment(0)
H
38

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

Howlett answered 9/5, 2016 at 20:22 Comment(1)
So if I understand this correctly, continue is saying "next value in loop please"? And only "continue" if the if statement is False?Larrikin
A
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.

Adhibit answered 9/5, 2016 at 20:34 Comment(3)
It isn't limited to None and 0. It will continue if the element is Falsey.Efficiency
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.Deponent
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 <some value> will be True only if the value of <some value> is any falsey values like None, False, empty string('') or 0. 2) if None means always FalseAdhibit
L
9

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.

Luzern answered 9/5, 2016 at 20:24 Comment(0)
T
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.

Turtle answered 9/5, 2016 at 20:23 Comment(0)
M
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.

Miserable answered 9/5, 2016 at 20:22 Comment(0)
B
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.

Burbage answered 9/5, 2016 at 20:20 Comment(0)
T
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.

Tem answered 12/5, 2023 at 12:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.