Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C three-way comparison of identical values results to NO - why?

Considering that all three variables have identical values, one would expect the following comparison to result in YES:

NSUInteger count1 = 2;
NSUInteger count2 = 2;
NSUInteger count3 = 2;
BOOL countEqual = (count1 == count2 == count3);
// but: countEqual = NO

Alas countEqual is NO and I'd like to better understand why and whether this particular issue also appears in C or C++ code?

My guess is:

(count1 == count2) --> YES (1)
(YES == count3) or (1 == count3) --> NO (0)
like image 964
LearnCocos2D Avatar asked Jan 28 '26 07:01

LearnCocos2D


1 Answers

Your guess is exactly correct, it will take the result from the first comparison, and compare it to the 3rd value. To do this you would need to do

countEqual = (count1 == count2) && (count1 == count3);

like image 158
Joshua Weinberg Avatar answered Jan 30 '26 21:01

Joshua Weinberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!