IF statement and OR condition in it | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

IF statement and OR condition in it

Hello, guys! Why does this y==“...”|| y==“...” works? And this one y==“...”|| “...” doesn’t? Ps. Please don’t pay attention to comments above-that is for another projects. https://code.sololearn.com/WgkZsYQ9KIFy/?ref=app

29th Sep 2018, 11:44 AM
Никита Шиляев
Никита Шиляев - avatar
5 Answers
+ 1
The problem is that in most programming languages, any string that is not empty is considered 'true' . if y == 'hello' || 'hi' is the same thing as if y == 'hello' || true. Since the right operand is true, the whole expression will always be true. So if y == 'hello' || 'hi' doesn't really check anything.
29th Sep 2018, 2:08 PM
Anna
Anna - avatar
30th Sep 2018, 8:36 AM
Anna
Anna - avatar
+ 1
if theres only a value in a condition, like in y == "a" || 1, it is always true, as 1 counts as true. you can replace 1 with every value that is not 0 and it should always return true. but if the value is 0, it is false as 0 represents false.
29th Sep 2018, 11:56 AM
Kevin Dietrichstein
Kevin Dietrichstein - avatar
+ 1
Anna, thank you for your answer! Thus, using OR condition with strings I should do y==“...” || y==“...” right?
30th Sep 2018, 8:35 AM
Никита Шиляев
Никита Шиляев - avatar
0
Kevin, thank you a lot for your answer but the question is how to make an abridge form y==“hello”||”hi” work as y==“hello” || y==“hi”. Can you answer please?
29th Sep 2018, 1:36 PM
Никита Шиляев
Никита Шиляев - avatar