need help with logics

General project discussion / help
Did you make a robotic coffee pot which implements HTCPCP and decafs unauthorized users? Show it off here!

Moderator: phalanx

Post Reply
alex.nev
Posts: 1
Joined: Wed Oct 03, 2018 3:18 pm

need help with logics

Post by alex.nev » Wed Oct 03, 2018 3:20 pm

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!

jremington
Support Volunteer
Posts: 2324
Joined: Fri Jun 15, 2007 9:41 pm
Location: Eugene, Or

Re: need help with logics

Post by jremington » Thu Oct 04, 2018 7:55 am

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
DanV
Posts: 393
Joined: Sat Apr 25, 2015 9:29 am

Re: need help with logics

Post by DanV » Thu Oct 04, 2018 8:20 am

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.

jremington
Support Volunteer
Posts: 2324
Joined: Fri Jun 15, 2007 9:41 pm
Location: Eugene, Or

Re: need help with logics

Post by jremington » Thu Oct 04, 2018 1:59 pm

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
DanV
Posts: 393
Joined: Sat Apr 25, 2015 9:29 am

Re: need help with logics

Post by DanV » Thu Oct 04, 2018 2:34 pm

That's really what I was implying - don't count on what you may think you know about operator precedence.

Post Reply