Page 1 of 1

need help with logics

Posted: Wed Oct 03, 2018 3:20 pm
by alex.nev
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!

Re: need help with logics

Posted: Thu Oct 04, 2018 7:55 am
by jremington
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.

Re: need help with logics

Posted: Thu Oct 04, 2018 8:20 am
by DanV
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.

Re: need help with logics

Posted: Thu Oct 04, 2018 1:59 pm
by jremington
You can, in fact, count on operator precedence. Guaranteed.

But most people don't bother to learn the rules that the compiler STRICTLY follows.

Re: need help with logics

Posted: Thu Oct 04, 2018 2:34 pm
by DanV
That's really what I was implying - don't count on what you may think you know about operator precedence.