Consecutive 'if' statements
- by Ben Packard
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.