Go Down

Topic: IF  with AND and OR fuctions (Read 281317 times) previous topic - next topic

sathopper

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

robtillaart


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.

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

becomes
Code: [Select]
if ((val > 100) && (val < 140))
{
....
}

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

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

lloyddean

#2
Dec 02, 2010, 06:06 pm Last Edit: Dec 02, 2010, 11:38 pm by lloyddean Reason: 1
C++ Keyword Synonyms

Code: [Select]

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


EDIT:  What the above means is that:

Code: [Select]
IF (VAL > 100 AND VAL < 140) THEN ...

can be written as:

Code: [Select]
if ( val > 100 and val < 140 )
{
...
}


sathopper

Quote
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 :) :) :)

foxbat

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

robtillaart

Quote
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 :)

Quote
parenthesis-blindness

Ever debugged LISP code?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up