Is Pass the Same as Continue Python
6 Answers 6
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
answered May 9, 2016 at 20:22
NeoNeo
3,274 2 gold badges 17 silver badges 32 bronze badges
1
'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.
answered May 9, 2016 at 20:34
AnoopAnoop
1,405 10 silver badges 20 bronze badges
3
-
It isn't limited to
None
and0
. It will continue if the element is Falsey.May 9, 2016 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.
May 15, 2016 at 0:19
-
1) Yes.
if not 1
meansFalse
. In this case condition will fail and execution will continue withprint
statement. This is the reason for printing 1 in your code.If not <some value>
will beTrue
only if the value of<some value>
is any falsey values like None, False, empty string('') or 0. 2)if None
means alwaysFalse
May 15, 2016 at 18:48
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.
answered May 9, 2016 at 20:23
C14LC14L
11.6k 4 gold badges 34 silver badges 50 bronze badges
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.
answered May 9, 2016 at 20:24
Vedang MehtaVedang Mehta
2,164 8 silver badges 22 bronze badges
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.
answered May 9, 2016 at 20:22
WreckeRWreckeR
409 3 silver badges 12 bronze badges
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.
answered May 9, 2016 at 20:20
SpadeSpade
2,153 1 gold badge 18 silver badges 29 bronze badges
pruittspecculp1971.blogspot.com
Source: https://stackoverflow.com/questions/37124717/if-pass-and-if-continue-in-python
So if I understand this correctly,
continue
is saying "next value in loop please"? And only "continue" if theif
statement isFalse
?Mar 10, 2021 at 19:26