简体   繁体   English

链接多个大于/小于运算符

[英]Chaining multiple greater than/less than operators

In an if statement I want to include a range, eg:if语句中,我想包含一个范围,例如:

if(10 < a < 0)

but by doing so, I get a warning "Pointless comparison".但这样做,我得到一个警告“毫无意义的比较”。 However, this works fine without any warning:但是,这可以正常工作而没有任何警告:

if(a<10 && a>0)

Is the first case possible to implement in C?第一种情况是否可以在 C 中实现?

Note that the original version if(10 < a < 0) is perfectly legal.请注意,原始版本if(10 < a < 0)是完全合法的。 It just doesn't do what you might (reasonably) think it does.它只是没有做你可能(合理地)认为它做的事情。 You're fortunate that the compiler recognized it as a probable mistake and warned you about it.您很幸运,编译器将其识别为可能的错误并警告您。

The < operator associates left-to-right, just like the + operator. <运算符从左到右关联,就像+运算符一样。 So just as a + b + c really means (a + b) + c , a < b < c really means (a < b) < c .所以就像a + b + c真的意味着(a + b) + c一样, a < b < c真的意味着(a < b) < c The < operator yields an int value of 0 if the condition is false, 1 if it's true. <运算符在条件为假时产生一个 int 值,如果条件为真,则为 1。 So you're either testing whether 0 is less than c, or whether 1 is less than c.因此,您要么测试 0 是否小于 c,要么测试 1 是否小于 c。

In the unlikely case that that's really what you want to do, adding parentheses will probably silence the warning.在不太可能的情况下,这确实是您想要做的,添加括号可能会使警告静音。 It will also reassure anyone reading your code later that you know what you're doing, so they don't "fix" it.它还将让以后阅读您的代码的任何人放心,您知道自己在做什么,因此他们不会“修复”它。 (Again, this applies only in the unlikely event that you really want (a < b) < c) .) (同样,这仅适用于您真正想要(a < b) < c)的不太可能发生的情况。)

The way to check whether a is less than b and b is less than c is:检查a是否小于bb是否小于c的方法是:

a < b && b < c

(There are languages, including Python, where a < b < c means a<b && b<c , as it commonly does in mathematics. C just doesn't happen to be one of those languages.) (有些语言,包括 Python,其中a < b < c表示a<b && b<c ,就像它在数学中通常所做的那样。C 恰好不是这些语言之一。)

这是不可能的,您必须像在案例 2 中那样拆分支票。

No it is not possible.不,这是不可能的。
You have to use the second way by splitting the two conditional checks.您必须通过拆分两个条件检查来使用第二种方式。

The first does one comparison, then compares the result of the first to the second value.第一个进行一次比较,然后将第一个值的结果与第二个值进行比较。 In this case, the operators group left to right, so it's equivalent to (10<a) < 0 .在这种情况下,运算符从左到右分组,因此相当于(10<a) < 0 The warning it's giving you is really because < will always yield 0 or 1. The warning is telling you that the result of the first comparison can never be less than 0, so the second comparison will always yield false.它给你的警告实际上是因为<总是会产生 0 或 1。警告是告诉你第一次比较的结果永远不会小于 0,所以第二次比较总是会产生 false。

Even though the compiler won't complain about it, the second isn't really much improvement.尽管编译器不会抱怨它,但第二个并没有太大的改进。 How can a number be simultaneously less than 0, but greater than 10?一个数如何同时小于 0,但大于 10? Ideally, the compiler would give you a warning that the condition is always false.理想情况下,编译器会警告您条件始终为假。 Presumably you want 0<a<10 and a>0 && a<10 .大概你想要0<a<10a>0 && a<10

You can get the effect of the second using only a single comparison: if ((unsigned)a < 10) will be true only if the number is in the range 0..10.您可以仅使用单个比较来获得第二个效果: if ((unsigned)a < 10)仅当数字在 0..10 范围内时才会为真。 A range comparison can normally be reduced to a single comparison with code like:范围比较通常可以简化为使用如下代码进行的单个比较:

if ((unsigned)(x-range_start)<(range_end-range_start))
    // in range
else
    // out of range.

At one time this was a staple of decent assembly language programming.曾经,这是体面的汇编语言编程的主要内容。 I doubt many people do it any more though (I certainly don't as a rule).我怀疑很多人会再这样做(我当然不会作为一项规则)。

As stated above, you have to split the check.如上所述,您必须拆分支票。 Think about it from the compiler's point of view, which looks at one operator at a time.从编译器的角度考虑它,一次只查看一个运算符。 10 < a = True or False. 10 < a = 真或假。 And then it goes to do True/False < 0, which doesn't make sense.然后它会执行 True/False < 0,这是没有意义的。

不,这不是 if 语句的有效语法,它应该有一个有效的常量表达式,或者其中可能有逻辑运算符,并且仅在括号中的表达式计算结果为真或非零值时执行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM