if ( var) ?

I am a bit confused with this part of the keypad code, and its' comment

if(key) // same as if(key != NO_KEY)- did something change?

I tried checking it in the reference section for the if statement, and it is not mentioned, but it does say , while warning about using a single = , that

" This is because C evaluates the statement if (x=10) as follows: 10 is assigned to x (remember that the single equal sign is the assignment operator), so x now contains 10. Then the 'if' conditional evaluates 10, which always evaluates to TRUE, since any non-zero number evaluates to TRUE. "

Which seems to mean that if(key) is any value other than zero, it will be TRUE, whether it has changed or not ?

"!=" means "not equal", you did not look the correct thing up.

At the same time:

"if(key) is any value other than zero, it will be TRUE"

if the value of key is not 0, then the If condition passes.

its the " did something change? " comment that has me confused...

If my key is always = 1, then the if statement will be true, even though nothing has changed, but if it changes to 0, then it will be false, even though something changed ??

"// same as if(key != NO_KEY)- did something change?"

The whole thing is a comment for the user, saying in effect
"if (key)" is another way to write "if (key != NO_KEY)"
so it would appear that NO_KEY would have a value of 0 or 1.
so the value of key is being compared to 0 or 1, or more simply it will function as
if (0), or if(1).

Hmm the brains a bit wooly at 5am, perhaps I will ask the wife when she gets up, her mind works like this :slight_smile:

if NO_KEY were to be the previous value of key, I could understand the idea of " did something change? " but it is not declared or used in the example.

It looks to me as though this is simply going to be true for any keypress ( other than 0 ) even if it is the same value, so where is the " did something change? "

Unless the keypad.h does not allow a subsequent value to be the same?
Which could explain it, the comment would be more about the operation of the library, rather than about the if(key) which is what I was trying to understand ?

The keypad library essentially implements a one-shot functionality. When you call getkey() it will only return a NEW keypress. It will not repeatedly return the same keypress.

So, for example, you press and hold the 5 key. The first getkey() call you make after the 5 key is pressed will return 5. Every subsequent getkey() will return zero until the 5 key is released and a second keypress occurs (even a second press of the 5 key will register as a new keypress). For each discrete keypress, getkey() will only return a single corresponding value for that keypress, no matter how many times you call getkey() for that keypress.

Yes thats what I finally came to, the confusion was that I was reading the comment as being just related to its line, ( if ( key ) ),not the previous functions of the keypad library.
Often the comment precedes the code it refers to.

Thanks guys, I can go back to bed now !