Boolean Operators..&& (AND) is working but ! (NOT) is'nt working

Hi there,
I'm trying to use NOT but unfortunately even with the codes below, arduino never gets into the second if.

 if((readString.indexOf("test") >0) && (readString.indexOf("test2") >0))
 { 
   //1   
 }//checks for test words

  if((!readString.indexOf("test") >0) && (!readString.indexOf("test2") >0)) 
{  
   //2   
  }  //check if there aren't any test words

No1 is working like a charm but No2 isn't for some reason.
When I check for 1, I type "test" and "test2", but when I check for 2 I type other words. It never gets into 2.
Can anyone explain to me why this is happening?

You are applying the not to the wrong thing. What you want it !(readString.indexOf("test") >0).

Although, I think that is atrocious code. It's much better to change the value that readString.indexOf() is being compared to, in my opinion.

nathanas:

  if((!readString.indexOf("test") >0) && (!readString.indexOf("test2") >0))

See the operator precedence: C++ Operator Precedence - cppreference.com

Note that ! is a higher precedence than >.

Consider what happens when we parenthesize by precedence:

if( !(readString.indexOf("test")) >0 && !(readString.indexOf("test2")) >0 )

PaulS is right too:

readString.indexOf("test")) <= 0

is a much more readable test and faster to type.

Thanks for sharing guys!
Made it working. :slight_smile: