1

Alright, so how "nice" does C play with using multiple logic operators in one if statement? For example, if I want to compare two points, to see if one is adjacent to the other, would it work to use if(X1 == X2 && Y1-- == Y2 || Y1++ == Y2 || Y1 == Y2 && X1-- == X2 || X1++ == X2) or do I need to break it up into 4 else if statements?

1
  • 1
    Not related to your problem, but modifying the X and Y values with the ++ and -- operators is probably not a good idea. Instead of writing e.g. Y1++ == Y2 I'd rather write Y1 + 1 == Y2 Commented Oct 13, 2014 at 14:53

1 Answer 1

4

You can absolutely use the way you are doing as long as you use () properly to determine which conditions are the part of the outer parathenses and which one is the part of the inner one. For instance, assuming your condition is true only if X1 == X2 and Y1-- == Y2 || Y1++ == Y2 || Y1 == Y2 and X1-- == X2 || X1++ == X2. then use parathenses properly, like this:

if( (X1 == X2) && (Y1-- == Y2 || Y1++ == Y2 || Y1 == Y2) && (X1-- == X2 || X1++ == X2) )

This condition without parentheses makes it very complicated to understand, and you are likely to make an error.

Sign up to request clarification or add additional context in comments.

1 Comment

So, then if parens work here to indicate order, why have I gotten errors in the past when I used parens to indicate OOS for arithmetic when doing an assignment?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.