Reputation: 443
I wanted to see if, instead of writing if (row > -1 && row < _rows)
I could shorten it to if (-1 < row < _rows)
. I used the below test to find out (it didn't return any compile errors).
int x = 5;
BOOL test1 = NO;
BOOL test2 = NO;
BOOL test3 = NO;
if (x < 6 && x > 4) {
test1 = YES;
}
if (4 < x < 6) {
test2 = YES;
}
if (5 < x < 7) {
test3 = YES;
}
All three tests came up YES
, including the 3rd one which, if my syntax was correct, should've been NO
. It looks like this isn't valid syntax, but my question is, what is the compiler actually doing where the syntax (5 < 5 < 7)
returns true?
Upvotes: 0
Views: 151
Reputation: 452
If you want it to not use the logical and operator at all, this should work:
5 < x == 1 == (x < 7)
This evaluates to 1 == 1 == 1 , which is 1 == 1, which is a true value. The parentheses are needed because of how the compiler reads left to right.
Upvotes: 0
Reputation: 5131
I think it's because 5 < x
returns false, which casts to 0, which is less than 7.
Upvotes: 3