+ 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
5 Réponses
+ 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.
+ 3
Никита Шиляев Exactly!
+ 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.
+ 1
Anna, thank you for your answer! Thus, using OR condition with strings I should do y==“...” || y==“...” right?
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?