Order of operations in Objective-C if statement
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:
or 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.
Sir, e^iπ + 1 = 0, hence God exists; reply!
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.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| z-order for map elements and sprites in cocos2d | brokenPlatypus | 3 | 5,527 |
Apr 27, 2009 03:42 PM Last Post: LetoK |
|
| Isometric Alpha Draw Order. | superflat | 1 | 2,639 |
Feb 4, 2009 06:43 AM Last Post: ThemsAllTook |
|

