Go Down

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

sathopper

Dec 02, 2010, 05:02 pm
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

#1
Dec 02, 2010, 05:26 pm

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)) { ....}`

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 pmLast 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

#3
Dec 02, 2010, 08:20 pm
Quote

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

It works perfect now. Many thanks

foxbat

#4
Dec 02, 2010, 10:40 pm
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

#5
Dec 02, 2010, 11:23 pm
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