SparkFun Forums 

Where electronics enthusiasts find answers.

General project discussion / help
Did you make a robotic coffee pot which implements HTCPCP and decafs unauthorized users? Show it off here!
By alex.nev
#200552
hello evryone!
i`m a bit confused with the next thing. So i have x and y coords. I need do do an IF expression when both conditions are performed. Condition for X is when its in a range from 0,00 to 100,00 or over 230,00 and at the same time Y is in the range from 0,00 to 20,00 or over 180 then do {}

i`ve made it like

if (x<=100 || x>=230 && (y>=180 || y<20))

{
}

But you know its not working lika a charm...

Maybe there is a better solution for such logic like using an array? Im a newbie so don kick me too much))) Thanks guys!
By jremington
#200557
Take a look at logical operator precedence and then decide if you are using the parentheses correctly.

Hint: since you are treating X and Y differently, for the same basic logical test, the answer is no.
User avatar
By DanV
#200558
You need more parenthesis:

if (x<=100 || x>=230 && (y>=180 || y<20))

if ((x<=100) || ((x>=230) && ((y>=180) || (y<20))))

You must be very precise about what comparison is being done where - leave no doubt.
Don't count on operator precedence, just brute force it.
By jremington
#200561
You can, in fact, count on operator precedence. Guaranteed.

But most people don't bother to learn the rules that the compiler STRICTLY follows.
User avatar
By DanV
#200562
That's really what I was implying - don't count on what you may think you know about operator precedence.