Conditional statement in Objective-C with two comparison operators in one sentence

I’ve been studying Objective-C recently, and I found code for using the accelerometer in an iPhone app. It works great; however, there is one if-statement in the code that I just cannot understand (both the meaning and why it works). The specific fragment is as follows:

if (0.2f < deviceTilt.y > -0.2f){position.x = 0;} 

I just can’t understand the condition, and before I have not seen the use of two comparison operators in one separate sentence.

Hope someone can help me!

PS: the whole project can be found at this link: http://www.ifans.com/forums/showthread.php?t=151394

+4
source share
2 answers

This is certainly not typical, and most people will not like it. To really understand what is happening, you must understand the priority of operator C. See: http://www.swansontec.com/sopc.html .

We analyze the statement, knowing that the legend is associated from left to right:

<p> 1) 0.2 <deviceTilt.y. This is either true (which equals 1) or false (which equals 0)

2) Result (1)> -0.2f. That should always be true.

So this is the same as (1) or always true

+2
source

This is rated as:

let y := .1

 if ((.2 < y) > -.2) if (false > -.2) 

false is treated as int

 if (0 > -.2) if (true) 

let y := .3

 if ((.2 < y) > -.2) if (true > -.2) 

true is treated as int

 if (1 > -.2) if (true) 

-> always true


Most likely it meant:

 if ((.2 < y) && (y > -.2)) 
+3
source

Source: https://habr.com/ru/post/1397702/


All Articles