Reputation: 11855
In Objective C does the Conditional-AND operator(&&) show short circuit behavior as in Java? (Meaning right hand part will only be evaluated if left hand part is true) Strangely I couldn't find info on it.
Upvotes: 1
Views: 100
Reputation: 3830
Yes. You can test it like I did:
NSArray *arr=nil;
if ((arr!=nil)&&([[arr objectAtIndex:0] isEqualToString:@"testing"])) {[arr addObject:@"tested"];}
Upvotes: 1
Reputation: 19608
anyway... you can test it by yourself, using a fake function that will raise an exception if invoked... like:
if (foo && [self raiseError]) {
}
if foo is false the error won't be raised
Upvotes: 1
Reputation: 18488
Yes Objective C also uses short circuit evaluation, and will only evaluate what is necessary, just like Java and C.
Upvotes: 1
Reputation: 170859
Objective-c is a strict superset of c and basically follows the same standard. So as AND operator is short-circuited in c, it is the same in objective-c
Upvotes: 4