大于的用法,小于运营 [英] Usage of greater than, less than operators

查看:113
本文介绍了大于的用法,小于运营的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我想包括一系列的,即如果语句;

In an if statement I want to include a range ie;

案例1:

if(10 < a < 0)

但这样做,我得到一个警告毫无意义的比较,然而,

but by doing so, I get a warning "Pointless comparison" however,

案例2:

if(a>10 && a<0) //works fine without any warning

我的问题是在C实现第一种情况可能吗?

My question is is the first case possible to implement in C?

推荐答案

请注意,原来的版本是完全合法的;它只是没有做什么你可能会(合理的),认为它。你是幸运的编译器可识别它作为一个可能的错误,并警告你一下吧。

Note that the original version is perfectly legal; 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.

&LT; 运营商联营左到右,就像 + 运营商。因此,正如 A + B + C 的真正含义(A + B)+ C A&LT; B&LT; ç的真正含义(A&LT; B)&LT; ç。在&LT; 运营商,如果条件为假,1,如果这是真的产生0一个int值。所以,你要么测试是否0小于C,还是1比C。

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. The < operator yields an int value of 0 if the condition is false, 1 if it's true. So you're either testing whether 0 is less than c, or whether 1 is less than c.

在不可能的情况下这真的是你想要做什么,添加括号可能会沉默的警告。它也将打消任何阅读您的code以后,你知道你在做什么,所以他们没有修复了。 (同样,这仅适用于你的万一真的的希望(A&LT; b)c为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 B 小于 C 是:

The way to check whether a is less than b and b is less than c is:

a < b && b < c

这篇关于大于的用法,小于运营的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆