100 <= x <= 150 作为 if () 中的参数,表现得很有趣

发布于 2024-12-29 02:32:43 字数 354 浏览 1 评论 0 原文

我有一个 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 中不起作用吗?任何帮助表示赞赏。谢谢

I have an if statement followed by several else if statements. All of the if/else if statements have an argument structured something like this:

if (100 <= x <= 149) //do this
else if (150 <= x <= 199) //do that
else if ...etc...

However, for some reason only the first if statement ever gets executed. X can be 200 and only the first if statement will be recognized.

I'm not sure why it isn't moving on to the next else if statement when X doesn't fit the argument of the preceding statement. Does this not work in Obj-C? Any help is appreciated. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

醉城メ夜风 2025-01-05 02:32:43

您需要重新表述以下语句:

if (x >= 100 && x <= 149) {
} else if (x >= 150 && x <= 199) {
} ...

您的第一个 if 的计算方式如下:

if ((100 <= x) <= 149)

让我们看看它是如何计算的:

  • 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:

if (x >= 100 && x <= 149) {
} else if (x >= 150 && x <= 199) {
} ...

Your first if is evaluated like:

if ((100 <= x) <= 149)

Let's have a look how that evaluates:

  • If x = 200, then (100 <= 200) is true and thus evaluates to the value 1 (which means true). And then 1 <= 149 is also true.
  • If x has a value smaller than 100, for example 10, then (100 <= 10) is false and thus evaluates to the value 0 (which means false). Again, 0 <= 149 is true.

So regardless of the value of x, the whole expression will always be true.

笙痞 2025-01-05 02:32:43

C 不是数学,所以

if (100 <= x <= 149)

不同

if (100 <= x &&  x <= 149)

与您的意思 。前者始终为真,因为 100 <= x <= 149 留下

((100 <= x) <= 149)

两种可能性:(1 <= 149)(0 < ;= 149)。两者都是正确的。

C is not math, so

if (100 <= x <= 149)

is NOT the same as

if (100 <= x &&  x <= 149)

Which is what you meant. The former will be true always, because 100 <= x <= 149 becomes

((100 <= x) <= 149)

leaving two possibilities: (1 <= 149) or (0 <= 149). Both are always true.

蓝戈者 2025-01-05 02:32:43

像这样的链式比较在基于 C 的语言中不起作用。或者更确切地说,他们确实这样做了,但不是你所期望的那样。

100 <= x <= 149 的计算结果为 (100 <= x) <= 149。如果 x 超过 100,则 (100 <= x) 将计算为 true,或 1。如果 x 小于 100,则为 false,或者为 0。无论哪种情况,0 <= 1491 <= 149 都为 true,因此整个表达式为 true。

要解决此问题,请将您的条件更改为如下所示:

if (100 <= x && x <= 149) 

这将使其按您的预期工作。

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. If x is over 100, then (100 <= x) will evaluate to true, or 1. If x is under 100, it's false, or 0. In either case, 0 <= 149 or 1 <= 149 is true, so the overall expression is true.

To fix this, change your conditions to look like this:

if (100 <= x && x <= 149) 

That will make it work as you expect.

稍尽春風 2025-01-05 02:32:43

编译器看到由二元运算符制定的表达式。 <= 符号是二元运算符,就像=、>=、||、&& 等一样。

就像算术一样,编译器必须遵循一个优先顺序,评估围绕每个二元运算符形成的表达式。

在算术中,您可能熟悉这一点,如以下两个示例所示:

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.

初吻给了烟 2025-01-05 02:32:43

因为如果您将 200 或其他数字赋予 x,则 if (100 <= x <= 149)if(1<=149) 相同。这始终是正确的。

例如。

x=1

100<=1 为 false,因此您得到 if(0<=149),这是 true

x=200

100<=200 为 true,因此您得到 < code>if(1<=149) 这是 true

所以你总是得到 true。

所以你必须用另一种方式来做,就像这样

if(x>=100 && x<=149) ...

Because if (100 <= x <= 149) is the same as if(1<=149) if you give 200 or another number to x. And that is correct always.

For example.

x=1

100<=1 is false so you get if(0<=149) which is true

x=200

100<=200 is true so you get if(1<=149) which is true

So you always get true for it.

So you must do it in another way, like this

if(x>=100 && x<=149) ...
暖阳 2025-01-05 02:32:43

添加一些额外的括号可能会有所帮助。

if ((100 <= x) && (x <= 149)) //do this

Adding some additional parenthesis may help.

if ((100 <= x) && (x <= 149)) //do this
仄言 2025-01-05 02:32:43

我不认为你可以在 Objective-C 中编写这样的数学函数...尝试将它们分开并与 && 结合起来陈述:

if ( (100 <= x) && (x <= 149) ) { // "&&" = and, "||" = or, other math comparison operators are: <=, >=, <, >, ==, != (!= is does not equal))
//do this
} else if ( (150 <= x) && (x <= 199) ) {
//do that
} else if ...etc...

I don't think you can write math functions like this in objective-c... Try separating them and combining with an && statement:

if ( (100 <= x) && (x <= 149) ) { // "&&" = and, "||" = or, other math comparison operators are: <=, >=, <, >, ==, != (!= is does not equal))
//do this
} else if ( (150 <= x) && (x <= 199) ) {
//do that
} else if ...etc...
近箐 2025-01-05 02:32:43

您已经得到了很多答案,但我将再添加一个答案以涵盖另一个可能的混淆点。

在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,则值为 int1

鉴于此以及关系运算符的从左到右关联性,您的:

if (100 <= x <= 149)

被解析为:

if ((100 <= x) <= 149)

然后 100 <= x 计算结果为 int1 如果 x 大于或等于 100,否则计算结果为 int0,因此我们得到:

if (1 <= 149)

if (0 <= 149)

两者评估为 1 所以我们得到:

if (1)

如果 if 语句的表达式非零,则它会分支到“then”分支。

这可能令人惊讶,但整个语句的计算完全没有使用任何布尔值 - 全部都是用整数完成的。

为了实现您的预​​期,您需要:

if((100 <= x) && (x <= 149))

等等 - 它也不使用任何布尔值(&&是根据整数定义的)。

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 hold 0 & 1. Cocoa uses the type BOOL for boolean, which is defined as signed char. CoreFoundation uses the type Boolean which is defined as unsigned char. All three define YES/true as 1 and NO/false as 0, while C itself treats any non-zero value as true.

The relation operators such as <, <= etc. are defined to return the int (yes, none of the booleans, not even _Bool) value 0 if the relation is false, and the int value 1 if the relation is true.

Given this and the left-to-right associativity of relational operators your:

if (100 <= x <= 149)

is parsed as:

if ((100 <= x) <= 149)

then 100 <= x evaluates to the int value 1 if x is greater than or equal to 100, otherwise it evaluates to the int value 0, so we get:

if (1 <= 149)

or

if (0 <= 149)

both of these evaluate to 1 so we get:

if (1)

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:

if((100 <= x) && (x <= 149))

etc. - which also doesn't use any booleans (&& is defined in terms of integers).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文