Objective C 中的条件语句在单个子句中具有两个比较运算符
我最近一直在学习 Objective C,并且遇到了一些在 iPhone 应用程序中使用加速度计的代码。它工作完美;然而,代码中有一个 if 语句我根本无法理解(其含义及其工作原理)。具体的块是这样的:
if (0.2f < deviceTilt.y > -0.2f){position.x = 0;}
我只是无法弄清楚条件,而且我以前没有见过在一个子句中使用两个比较运算符。
希望有人能帮助我!
PS:整个项目可以在这个链接中找到:http://www.ifans .com/forums/showthread.php?t=151394
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
其计算方式如下:
let
y := .1
false 被视为 int
let
y := .3
true 被视为 int
->总是正确的,
很可能这意味着:
This is evaluated like:
let
y := .1
false is treated as an int
let
y := .3
true is treated as an int
-> always true
most likely this was meant:
这当然是非典型的,大多数人不会喜欢它。要真正理解发生了什么,您必须了解 C 的运算符优先级。请参阅:http://www.swansontec.com/sopc.html。
让我们分析该语句,知道条件从左到右关联:
1) 0.2f < deviceTilt.y。这要么为真(即1),要么为假(即0)
2)(1)的结果> -0.2f。这应该永远是正确的。
所以这与 if(1) 相同或始终为 true
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