100 <= x <= 150 作为 if () 中的参数,表现得很有趣
我有一个 if
语句,后面跟着几个 else if
语句。所有 if/else if 语句都有一个如下结构的参数:
if (100 <= x <= 149) //do this
else if (150 <= x <= 199) //do that
else if ...etc...
但是,由于某种原因,只有第一个 if 语句被执行。 X 可以是 200,并且仅识别第一个 if 语句。
我不确定为什么当 X 不适合前一个语句的参数时,它不继续执行下一个 else if 语句。这在 Obj-C 中不起作用吗?任何帮助表示赞赏。谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您需要重新表述以下语句:
您的第一个
if
的计算方式如下:让我们看看它是如何计算的:
x = 200
, then(100
= 200)
为 true,因此计算结果为值1
(表示 true)。那么1 <= 149
也是如此。x
的值小于 100,例如10
,则(100 <= 10)
为 false,因此计算结果为该值0
(表示错误)。同样,0 <= 149
为真。因此,无论
x
的值如何,整个表达式始终为 true。You need to rephrase the statements like:
Your first
if
is evaluated like:Let's have a look how that evaluates:
x = 200
, then(100 <= 200)
is true and thus evaluates to the value1
(which means true). And then1 <= 149
is also true.x
has a value smaller than 100, for example10
, then(100 <= 10)
is false and thus evaluates to the value0
(which means false). Again,0 <= 149
is true.So regardless of the value of
x
, the whole expression will always be true.C 不是数学,所以
不同
与您的意思 。前者始终为真,因为
100 <= x <= 149
留下两种可能性:
(1 <= 149)
或(0 < ;= 149)
。两者都是正确的。C is not math, so
is NOT the same as
Which is what you meant. The former will be true always, because
100 <= x <= 149
becomesleaving two possibilities:
(1 <= 149)
or(0 <= 149)
. Both are always true.像这样的链式比较在基于 C 的语言中不起作用。或者更确切地说,他们确实这样做了,但不是你所期望的那样。
100 <= x <= 149
的计算结果为(100 <= x) <= 149
。如果x
超过 100,则(100 <= x)
将计算为 true,或1
。如果x
小于 100,则为 false,或者为0
。无论哪种情况,0 <= 149
或1 <= 149
都为 true,因此整个表达式为 true。要解决此问题,请将您的条件更改为如下所示:
这将使其按您的预期工作。
Chained comparisons like these don't work in C-based languages. Or rather, they do, but not how you'd expect.
100 <= x <= 149
gets evaluated as(100 <= x) <= 149
. Ifx
is over 100, then(100 <= x)
will evaluate to true, or1
. Ifx
is under 100, it's false, or0
. In either case,0 <= 149
or1 <= 149
is true, so the overall expression is true.To fix this, change your conditions to look like this:
That will make it work as you expect.
编译器看到由二元运算符制定的表达式。 <= 符号是二元运算符,就像=、>=、||、&& 等一样。
就像算术一样,编译器必须遵循一个优先顺序,评估围绕每个二元运算符形成的表达式。
在算术中,您可能熟悉这一点,如以下两个示例所示:
2+5*7 其计算结果为 2+(5*7) 或 37,因为乘法优先于加法。
2+3+21 按照从左到右读取项的顺序进行计算,因为没有其他优先规则。变成(2+3)+21。
因此 100<=x<=150 是一个类似类型的表达式,其中二元运算符全部相同,因此具有相同的优先级。再次,通过从左到右求值来解决这个问题,所以它变成 (100<=x)<=150。如果 x>=100,则括号中的项计算为 TRUE,其数值为 1。由于 1 小于 150,因此其余部分计算为 1<=150,如果 x 大于或等于 100,则其余项计算为 TRUE。另一方面,如果 x 小于 100,它也评估为 TRUE,因为第二次比较变为 0≤150,这是 TRUE。
如果您不确定二元运算符的优先顺序,则用括号将其分解的其他建议是正确的: (100<=x) && (x≤150)。也可以写成 100<=x && x<=150,因为值比较的优先顺序高于逻辑运算符。
The compiler sees an expression formulated from binary operators. The <= symbol is a binary operator, as are =, >=, ||, &&, and so forth.
Just as with arithmetic, there is an order of precedence that the compiler must follow, evaluating the expression formed around each binary operator.
In arithmetic, you are probably familiar with this, as with these two examples:
2+5*7 This evaluates to 2+(5*7) or 37, because multiplication has precedence over addition.
2+3+21 is evaluated in the order the terms are read from left to right, since there is no other precedence rule. It becomes (2+3)+21.
So 100<=x<=150 is an expression of a similar type, where the binary operators are all the same, and thus have the same precedence. Once again, this is resolved by evaluating it from left to right, so it becomes (100<=x)<=150. If x>=100 the term in parentheses evaluates to TRUE, which has a numeric value of 1. Since 1 is less than 150, the rest evaluates to 1<=150, or TRUE, if x is greater than or equal to 100. On the other hand, it also evaluates to TRUE if x is less than 100, because the second comparison becomes 0<=150, which is TRUE.
The other recommendations to break this down with parentheses are correct if you aren't sure of the order of precedence for binary operators: (100<=x) && (x<=150). You can also write it as 100<=x && x<=150 since the order of precedence for value comparisons is higher than for logical operators.
因为如果您将 200 或其他数字赋予 x,则
if (100 <= x <= 149)
与if(1<=149)
相同。这始终是正确的。例如。
100<=1
为 false,因此您得到if(0<=149)
,这是 true100<=200
为 true,因此您得到 < code>if(1<=149) 这是 true所以你总是得到 true。
所以你必须用另一种方式来做,就像这样
Because
if (100 <= x <= 149)
is the same asif(1<=149)
if you give 200 or another number to x. And that is correct always.For example.
100<=1
is false so you getif(0<=149)
which is true100<=200
is true so you getif(1<=149)
which is trueSo you always get true for it.
So you must do it in another way, like this
添加一些额外的括号可能会有所帮助。
Adding some additional parenthesis may help.
我不认为你可以在 Objective-C 中编写这样的数学函数...尝试将它们分开并与 && 结合起来陈述:
I don't think you can write math functions like this in objective-c... Try separating them and combining with an && statement:
您已经得到了很多答案,但我将再添加一个答案以涵盖另一个可能的混淆点。
在C& Obj-C 布尔(和字符)类型被视为整数类型,而调用语言中的情况并非如此。因此像
'z' * true
这样的表达式非常有意义!(现代)C 使用
_Bool
类型表示布尔值,该类型被定义为足够大以容纳0
& <代码>1。 Cocoa 使用BOOL
类型表示布尔值,它被定义为signed char
。 CoreFoundation 使用Boolean
类型,该类型定义为unsigned char
。所有三个都将YES
/true
定义为1
,将NO
/false
定义为0
,而 C 本身将任何非零值视为true
。关系运算符如
<
、<=
等被定义为返回int
(是的,没有布尔值,甚至没有如果关系为 false,则_Bool
) 值为0
;如果关系为 true,则值为int
值1
。鉴于此以及关系运算符的从左到右关联性,您的:
被解析为:
然后
100 <= x
计算结果为int
值1 如果
x
大于或等于 100,否则计算结果为int
值0
,因此我们得到:或
两者评估为
1
所以我们得到:如果
if
语句的表达式非零,则它会分支到“then”分支。这可能令人惊讶,但整个语句的计算完全没有使用任何布尔值 - 全部都是用整数完成的。
为了实现您的预期,您需要:
等等 - 它也不使用任何布尔值(
&&
是根据整数定义的)。You've already got a lot of answers, but I'll add one more to cover one other possible point of confusion.
In C & Obj-C the boolean (and character) types are treated as integer types, which is not the case in call languages. So expressions like
'z' * true
make perfect sense!(Modern) C uses the type
_Bool
for boolean, which is defined to be large enough to hold0
&1
. Cocoa uses the typeBOOL
for boolean, which is defined assigned char
. CoreFoundation uses the typeBoolean
which is defined asunsigned char
. All three defineYES
/true
as1
andNO
/false
as0
, while C itself treats any non-zero value astrue
.The relation operators such as
<
,<=
etc. are defined to return theint
(yes, none of the booleans, not even_Bool
) value0
if the relation is false, and theint
value1
if the relation is true.Given this and the left-to-right associativity of relational operators your:
is parsed as:
then
100 <= x
evaluates to theint
value1
ifx
is greater than or equal to 100, otherwise it evaluates to theint
value0
, so we get:or
both of these evaluate to
1
so we get:and the
if
statement branches to the "then" branch if it's expression is non-zero.It may be surprising, but the whole statement is evaluated without any use of booleans at all - it is all done with integers.
To achieve what you intended you need:
etc. - which also doesn't use any booleans (
&&
is defined in terms of integers).