I have a quick question about using logical operators in an if statement.
Currently I have an if statement that checks if x equals to 5 or 4 or 78:
if ((x == 5) || (x == 4) || (x == 78)) {
blah
}
And I was wondering if I could just condense all that to:
if (x == 5 || 4 || 78) {
blah
}
Sorry for such a basic question, I've just started learning C.
||
operators. Also your=
needs to be==
. – Svobodax == 4 || 5
parses as(x == 4) || 5
, while the shortcut you suggest would require it to parse asx == (4 || 5)
, which would require other, arguably more common operations (likex == 0 || y == 0
) to use more parentheses. – Nettle