Problems with multiple conditions are sometimes due to unexpected "order of operations" situations. Put lots of ()s into your expression to force the compiler to do the tests in the order you meant. E.g.
N.B. I AM NOT USING THE ARDUINO LANGUAGE (exactly) in my example, just to make my point more clearly...
Your example would depend on how you are interpreting it, eg:
if (x==5 or y==7 and z>3)
...could be interpreted in two ways, depending on what you are checking for:
if x equals 5 or y equals 7, and z is greater than 3
...as well as:
if x equals 5, or y equals 7 and z is greater than 3
A simple comma shift, but one that means everything. For the first, the correct (pseudo) code would be
if ((x==5 or y==7) and z>3)
...while the second would be:
if (x==5 or (y==7 and z>3))
You can't just place parentheses whereever you want (and putting them around singular tests, while it doesn't break anything, does interrupt the readability of the code) and expect things to work; you need to -think- about what you are checking for and formalize the logic tests in the order needed to make the test function properly.
/now watch - I've probably left a bug somewhere... ;D
You can't just place parentheses whereever you want (and putting them around singular tests, while it doesn't break anything, does interrupt the readability of the code) and expect things to work
Actually, this is the preffered way of writing logic statements.. (nowadays).
Thing is, extra brackets don't disrupt readability, all the coders not bothering to line up their value assignments and method calls.. or not putting the curly brackets on their own lines disrupts it a whole lot more.
Besides, I've found way too many mistakes in if statements because they didn't use enough brackets...
No, using a pair of brackets around each comparison is perfectly fine =)
Mind you, not experienced in the embedded world (yet)...
Though I pray coding styles aren't too different from the rest of the coding industry.
PS: wholeheartedly agree with the thinking part though, also a common pitfall for coders.
Your example would depend on how you are interpreting it, eg:
if (x==5 or y==7 and z>3)
...could be interpreted in two ways...
Ah! I chose my example well: That was (almost) exactly my point.
What matters is how the Arduino interprets the expression.... and that may not be the way the programmer interpreted it, but the programmer's interpretation is irrelevant! What matters is what the Arduino "thinks". Problems of this origin are easily avoided with brackets.
Now... if you were to say "let's avoid overly complex compound condition statements", I couldn't agree more... but people WILL use them, and if they use plenty of brackets, they probably will go less far wrong.