0

Question about print

print(false or true) >>true likewise, print(true or true) >>true Any explanation about why it always print true? I don’t understand how does printing a “or” statement makes sense

5th Apr 2019, 5:12 PM
John
John - avatar
3 Antworten
+ 5
because or operator returns first operand if it is true, and or operator returns second operand if the first operand is false Try print ( 2 or 0) and print ( "hi" or "hello") and print ( 0 or "this")
5th Apr 2019, 5:19 PM
Gordon
Gordon - avatar
+ 3
Yes. Yes, all strings and non-zero integer is true. For false, null and undefined are interpreted as false too. And operator operates similarly. If the first one is false, it returns the first operand; if the first operand is true, it returns the second operand.
6th Apr 2019, 6:15 AM
Gordon
Gordon - avatar
+ 2
Gordon Thanks for answering my question anyway. So in this case, print(0 or 2) 0 is false so it will print the second operand which is 2 so the first operand is the one who determine which to print. If my understanding is correct, is it just false and 0 that stand for false or there are other things that stand for false too? one more little question, so all the strings and intergers(not 0) stand for true?
6th Apr 2019, 5:21 AM
John
John - avatar