在 C 中, (x==y==z) 的行为是否符合我的预期?
我可以比较如下三个变量,而不是执行 if((x==y)&&(y==z)&&(z=x))
吗? [如果所有三个变量具有相同的值,则应执行 if 语句。这些是布尔值。]
if(debounceATnow == debounceATlast == debounceATlastlast)
{
debounceANew = debounceATnow;
}
else
{
debounceANew = debounceAOld;
}
Can I compare three variables like the following, instead of doing if((x==y)&&(y==z)&&(z=x))
? [The if statement should execute if all three variables have the same value. These are booleans.]
if(debounceATnow == debounceATlast == debounceATlastlast)
{
debounceANew = debounceATnow;
}
else
{
debounceANew = debounceAOld;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,事实并非如此。
x == y
转换为 int,产生0
或1
,并将结果与z
进行比较。因此,当且仅当(x 等于 y 并且 z 为 1)或(x 不等于 y 并且 z 为 0)x==y==z
才会产生 true /code>你想做的是
No, it does not.
x == y
is converted to int, yields0
or1
, and the result is compared toz
. Sox==y==z
will yield true if and only if(x is equal to y and z is 1) or (x is not equal to y and z is 0)
What you want to do is
否。相等性检查从左开始关联,逻辑结果作为数字进行比较,因此表达式
2 == 2 == 1
解析为(2 == 2) == 1
,这又给出1 == 1
并产生1
,这可能不是您想要的。No. The equality check associates from the left and the logical result is compared as a number, so that the expression
2 == 2 == 1
parses as(2 == 2) == 1
, which in turn gives1 == 1
and results in1
, which is probably not what you want.你实际上可以输入这样的内容:
You can actually type something like this: