Order of operations in Objective-C if statement

Member
Posts: 65
Joined: 2009.01
Post: #1
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;
        }
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #2
anywhere you have (a && b), if a is true it will check b, if a is false it will not look at b.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #3
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  z-order for map elements and sprites in cocos2d brokenPlatypus 3 6,355 Apr 27, 2009 03:42 PM
Last Post: LetoK
  Isometric Alpha Draw Order. superflat 1 2,966 Feb 4, 2009 06:43 AM
Last Post: ThemsAllTook