While....!=...||

I'm having a few problems trying to get a while statement working as intended. :-/

The offending code:

while (key != '*' || key != '#' || key != 'P') {
key = keypad.getKey();
}

:frowning:

I'm using the keypad library which is working correctly.

What I'm trying to do here is essentially trap the program in a loop until either *,# or P key is pressed.

So.......what's up with that code?
From what I can tell it should work.

It's just saying:
'While '*' is not pressed OR '#' is not pressed OR 'P' is not pressed, keep going around the loop'.

Any ideas anyone?

The Cageybee

Let me introduce you to Mr de Morgan:

Wow. That was quick!

Me thinks this may take some time to get my head aroud. Thinking time travel paradoxes ala Star Trek. :-/

Thanks!

The Cageybee

Hint: If it isn't OR, it may be AND.
Also, it may be better as a "do..while" loop, unless "key" already has a value.

Many thanks. Yep, needed changing to ANDs.

The Cageybee

Let me introduce you to Mr de Morgan

BOFH! Nice...

;D

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...

instead of....

if (x==5 or y==7 and z>3)...

use...

if ((x==5) or ((y==7) and (z>3)))...

tkbyd:

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.

:slight_smile:

/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. :slight_smile: