IF  with AND and OR fuctions

With my BASIC language programmed controllers I can use AND and OR.

example: IF (VAL > 100 AND VAL < 140) THEN ...

How can I solve this with the if function in the Arduino?

Thanks. :wink:

1 Like

you need to add () 's and a lot of them , further Arduino knows 2 types of AND and OR's the logical and the bitwise.

IF (VAL > 100 AND VAL < 140) THEN ...

becomes

if ((val > 100) && (val < 140)) 
{ 
....
}

See for more information http://www.arduino.cc/en/Reference/Boolean

1 Like

C++ Keyword Synonyms

and             &&
and_eq          &=
bitand          &
bitor           |
not             !
not_eq          !=
or              ||
or_eq           |=
xor             ^
xor_eq          ^=

EDIT: What the above means is that:

IF (VAL > 100 AND VAL < 140) THEN ...

can be written as:

if ( val > 100 and val < 140 )
{
 ...
}
3 Likes

See for more information http://www.arduino.cc/en/Reference/Boolean

Thanks for this link didn't now where to look.

It works perfect now. Many thanks :slight_smile: :slight_smile: :slight_smile:

you need to add () 's and a lot of them

Why? && and || are of a much lower precedence than any operator you would commonly use in a comparative expression. Overbracketing just leads to parenthesis-blindness in my opinion, though I concede that is partly a matter of style.

Why? && and || are of a much lower precedence than any operator you would commonly use in a comparative expression

True, but for those few times the precedence was higher it kept my debugging sessions short except the first time :slight_smile:

parenthesis-blindness

Ever debugged LISP code?