Stack Overflow Asked by Marius on February 25, 2021
Can someone explain how this While-loop actually works, how this piece of code just end up to result: 3 and 1, and 4 and 0. How? Thanks.
i = 2
j = 2
while i < 4 or j > 2:
i += 1
j -= 1
print(i, j)
Output:
3 1
4 0
I'm at home in my pyjamas, so I don't have the tools to check it out (and I'm too tired for theory), but here lies the big hairy monster:
They all give the same result as the op...
i = j = 2
while i < (4 or j) > 2:
i += 1
j -= 1
print(i, j)
print('-'*5)
i = j = 2
while i < (4 or j > 2):
i += 1
j -= 1
print(i, j)
print('-'*5)
i = j = 2
while (i < 4) or (j > 2): # Prefered way of doing things...
i += 1
j -= 1
print(i, j)
Output:
3 1
4 0
-----
3 1
4 0
-----
3 1
4 0
So how does i < 4 or j > 2
work?
Comparison operators have the priority and it is left to right. i < 4
, j > 2
are done first.
Then the logical operator or
is applied.
See: Operator precedence (Don't trust what you read on the internet !)
Now there's another big hairy monster:
When using the or
op, do we sill process the right operand when the left one is already True? Doesn't seem like it... But I don't have my tools to check if it behaves the same with simple values instead of functions.
def print_and_return(mah_var):
print(mah_var)
return mah_var
value = print_and_return(False) or print_and_return(True)
print('mah value!', value)
print('-'*5)
# Here, the right operand is ignored !
value = print_and_return(True) or print_and_return(False)
print('mah value!', value)
print('-'*5)
# Here, the comparison operand is ignored !
value = print_and_return(True) or print_and_return(True) > print_and_return(True)
print('mah value!', value)
Output:
False
True
mah value! True
-----
True
mah value! True
-----
True
mah value! True
So, first i < 4
is processed. Then or
is processed, which either returns the left operand value if true, else return the right operand value (trigger the processing of i > 2
).
Answered by Florian Fasmeyer on February 25, 2021
If you were expecting to see “2 2” first you must place the print statements before the arithmetic operations. As you’ve formally asked it to, the loop applies the operations until neither clause of the ‘or’ is true, printing the result each time before moving onto the next step.
Answered by kendfss on February 25, 2021
It seems you need a proper IDE which includes a debugger. That way you can step through your code line by line and see what happens to each variable. I suggest Pycharm. They have a free community edition.
Line number value of i value of j i < 4 j > 2 or of these
Program start -/- -/- -/-
Line 1 2 0 -/-
Line 2 2 2 -/-
Line 4 2 2 True False True
Line 5 3 2 -/-
Line 6 3 1 -/-
Line 7 ^print ^print
Line 4 3 1 True False True
Line 5 4 1 -/-
Line 6 4 0 -/-
Line 7 ^print ^print
Line 4 4 0 False False False
End of program ^jump out of while loop
Answered by Thomas Weller on February 25, 2021
On the first iteration, i == 2
and j == 2
. The condition i < 4
is true, while j > 2
is false. When you combine conditions with or
, it's true if either of the conditions is true, so the combined condition is true and we go into the loop body.
Then it adds 1 to i
and subtracts 1 from j
, and prints the new values 3 1
.
On the next iteration i < 4
is still true and j > 2
is still false. Since one of them is true the or
condition is true, so we go into the loop body.
Then it increments i
and decrements j
, and prints the new values 4 0
.
On the next iteration, i < 4
is false and j > 2
is false. Since both of the conditions are false the or
condition is also false. So the loop stops.
Answered by Barmar on February 25, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP