Spring
Spring

Reputation: 11855

Objective-C AND operand is shortcircuited?

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

Answers (4)

Henrik Erlandsson
Henrik Erlandsson

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

daveoncode
daveoncode

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

Oscar Gomez
Oscar Gomez

Reputation: 18488

Yes Objective C also uses short circuit evaluation, and will only evaluate what is necessary, just like Java and C.

Upvotes: 1

Vladimir
Vladimir

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

Related Questions