I’ve been learning Objective C lately, and I came across some code for using the accelerometer in an iPhone app. It works perfectly; however, there’s one if-statement in the code which I simply cannot understand (both the meaning and why it works). The specific chunk is this:
if (0.2f < deviceTilt.y > -0.2f){position.x = 0;}
I just can’t figure out the condition, and I hadn’t seen the use of two comparison operators in one single clause before.
Hope somebody can help me out!
PS: The whole project can be found in this link: http://www.ifans.com/forums/showthread.php?t=151394
This is certainly atypical and most people wouldn’t like it. To really understand what is going on, you have to understand C’s operator precedence. See: http://www.swansontec.com/sopc.html.
Let’s analyze the statement knowing that conditionals associate left-to-right:
1) 0.2f < deviceTilt.y. This is either true (which is 1) or false (which is 0)
2) The result of (1) > -0.2f. Which should always be true.
So this is the same as if(1) or always true