IF statement with two ANDed conditionals

To preface, I have been working with an Arduino for about a year now and I have this stupid problem(no, not my cousin Bill). I have an IF statement with two expressions that need to be true for the IF statement two be true. I've tried the following:

if(-100 <= dataIn <= 100 && Serial.available() == 0)


if((-100 <= dataIn <= 100) && (Serial.available() == 0))

and a few others out of desperation. I know I did this before but it is only analyzing the second expression (the Serial.available() one). Can somebody slap some sense into me?

Also, I just noticed spellcheck doesn't recognize "Arduino" in the posts.


(-100 <= dataIn <= 100)

does not do what you think. Each sub-expression should have only a single comparison:

((dataIn >= -100) && (dataIn <= 100))

There are, literally, thousands of web pages with tutorials on c/c++ expression syntax....

Ray L.

Thanks, I knew it was something stupid. I needed that. Just goes to show, smart (-ish in my case) people can do dumb things.

I would write:

if(! Serial.available() && (dataIn >= -100 && datain <= 100))

if NOT Serial.available() AND datain is between -101 and 101.

I would write

Serial.available() does not return a boolean, so I hate to see code written that way.

if(Serial.available() > 0 && (dataIn >= -100 && datain <= 100))

is how I would write it. That shows that you KNOW what the value that Serial.available() returns means.

Of course, I probably would NOT combine what are two completely unrelated clauses in one if statement.

if(Serial.available() > 0)
   if(dataIn >= -100 && datain <= 100)

is how I would really write it (if that made sense; it's hard to tell from a snippet).

Those compound IF statements can also catch you out if they are followed by an ELSE


Those compound IF statements can also catch you out if they are followed by an ELSE

Exactly. That's why I rarely use compound if statements, except to check a range. The else clauses of nested if statements can tell you exactly why a particular path was not chosen, without the need for even more if statements to sort that out.