iDevGames Forums

Full Version: Order of operations in Objective-C if statement
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In my collision testing I have this statement, which tests collision between an array of missiles and an asteroid. I don't want the collision test to be performed if the missile is not visible. So which order is correct? This:

Code:
if([[missiles objectAtIndex:i] isVisible] && [[missiles objectAtIndex:i] isCollidedWith:asteroid])
        {
            asteroid.isIntact = NO;
        }

or this?
Code:
if([[missiles objectAtIndex:i] isCollidedWith:asteroid] &&[[missiles objectAtIndex:i] isVisible])
        {
            asteroid.isIntact = NO;
        }
anywhere you have (a && b), if a is true it will check b, if a is false it will not look at b.
To expand on that, it won't even evaluate whatever expression is in b if a is false. In your example, it won't call your function. This is also true in the case of a || b, where if a is true b won't be evaluated. This is called short circuiting and is true in most languages I know of.
Reference URL's