Consecutive 'if' statements

Posted by Ben Packard on Stack Overflow See other posts from Stack Overflow or by Ben Packard
Published on 2010-05-03T04:30:59Z Indexed on 2010/05/03 4:38 UTC
Read the original article Hit count: 316

Filed under:
|
|

How can I check one thing AND THEN another, if the first is true?

For example, say I have a shopping basket object, and I only want to do something if the basket has been created AND it isn't empty.

I've tried:

if (basket && [basket numberOfItems >0])...

But the second condition is evaluated even if the first fails, resulting in a crash (presumably because i'm calling numberOfItems on an object that doesn't exist).

I can nest them, but this seems a bit ugly, and more to the point is problematic. Say I want to do one thing if the basket exists AND isn't empty, but another if either isn't true. That doesn't really work well in nested if statements.

© Stack Overflow or respective owner

Related posts about cocoa

Related posts about objective-c