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 Answer
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.
1 Comment
Phoxxent
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?
++and--operators is probably not a good idea. Instead of writing e.g.Y1++ == Y2I'd rather writeY1 + 1 == Y2