How to code/write plus or minus a

I am trying to figure how to code a line which essentially is:
"If x greater than or equal to y(+ or - 1)"
but how am I able to do that? I'm trying to figure out an appropriate notation but am struggling a bit so I would appreciate some help, thank you!

EDIT:
Apologies for the mistake, I was thinking two different things and mistook what I was originally going to ask. I meant to say how would I write the code for
"If x does not equal y(+ or -1)
Or in a diff notation, basically if x isn't equal to a value between y + 1 and y - 1:
"If x does not equal (y-1 < y < y +1)"

It is not entirely clear to me what you are trying to do. It would help if you provided examples with actual numerical values

`if (x >= (y-1))"

If I understand what you're trying to do... Subtract X from Y and check the absolute value.

Or you may need two conditions with an and (&&) or or (||) to check for a range of values... i.e. Greater than A and less than B, etc.

So specifically, the plus one is greater than the minus one, so all you have to do is check against the lower number. Not either or both. What John wrote.

Could you tell us the scenario you're trying to code for?

    if (abs(x - y) >= 1)

Sure, I had typed the wrong question on my part but what I meant to say, along with numerical values:

X and Y can be any arbitrary numerical value that comes from an input from an external component, like an ultrasonic sensor for eg.
If x = 5 and y = 7
I'd want to write a line that essentially follows:
If x does not equal + or - 1 y
Or with the values in
If 5 does not equal 7(+ or - 1; any value between the range of 8 and 6)

I hope this helps.

If the condition is satisfied then the code will carry on, but really I just need to know how write the part I wrote above in code.

Hello movz1804

Try this:

result= x==y?+1:-1;

Could you explain how it works?

Okay thank you, I'll try it out and see if it works

@movz1804

Is what you are looking for.

Do you understand the abs operation? Look it up in the reference.

1 Like

Yeah I think it is. Abs is just the absolute value of the difference? I've checked and it seems to fit what I need, just without the equal sign

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.