C++ 带有逻辑 OR 的 IF 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33422793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IF statement with logical OR
提问by Mike York
if(1 == 2 || 4)
{
cout<<"True";
}
else
{
cout<<"False";
}
This is how I read the above. If 1 is equal to 2 or 4, then print true. Otherwise, print false. When this is executed though... true is printed. Obviously I'm misunderstanding something here. 1 is not equal to 2 or 4. Wouldn't that make it false?
这就是我阅读上述内容的方式。如果 1 等于 2 或 4,则打印 true。否则,打印 false。执行此操作时...打印 true。显然我在这里误解了一些东西。1 不等于 2 或 4。这不会使它成为假吗?
回答by cadaniluk
Yeah, I've made the same mistake.
是的,我也犯过同样的错误。
Read the sentence again:
再读一遍句子:
If 1 is equal to 2 or 4, then print true.
如果 1 等于 2 或 4,则打印 true。
The "2" and "4" both refer to the "If 1 is equal to [...]." That means, the sentence is just an abbreviation of
“2”和“4”都是指“如果 1 等于 [...]”。这意味着,这句话只是一个缩写
If 1 is equal to 2 or 1 is equal to4, then print true.
如果 1 等于 2 或1 等于4,则打印 true。
This leads us to the if
-clause
这将我们引向 -if
子句
if (1 == 2 || 1 == 4)
instead.
反而。
1 == 2 || 4
is true because (1 == 2) == false
ORed with 4 == true
, yields true (false OR true = true).
1 == 2 || 4
为真,因为(1 == 2) == false
与 进行或运算4 == true
,结果为真(假或真 = 真)。
回答by David Schwartz
This is how I read the above. If 1 is equal to 2 or 4, then print true. Otherwise, print false. When this is executed though... true is printed. Obviously I'm misunderstanding something here. 1 is not equal to 2 or 4. Wouldn't that make it false?
这就是我阅读上述内容的方式。如果 1 等于 2 或 4,则打印 true。否则,打印 false。执行此操作时...打印 true。显然我在这里误解了一些东西。1 不等于 2 或 4。这不会使它成为假吗?
No, 1 is equal to 2 or 4. 4
is true (because it's not zero). So anything "or 4" is also true. Therefore "1 is equal to 2 or 4" is true.
不,1 等于 2 或 4。4
是真的(因为它不是零)。所以任何“或 4”也是正确的。因此“1 等于 2或 4”是正确的。
回答by Derek Schuster
It's evaluating to true because if you check for the boolean value of just an integer (like you're doing with the number 4), it will evaluate to true. Like the people said above, you have to split it up.
它的评估结果为真,因为如果您检查一个整数的布尔值(就像您对数字 4 所做的那样),它将评估为真。就像上面的人说的,你必须把它分开。
回答by Vlad from Moscow
In this your phrase
在这个你的短语中
If 1 is equal to 2 or 4, then print true
如果 1 等于 2 或 4,则打印 true
you need to add a pair of words that to get a correct equivalent C++ phrase
您需要添加一对单词以获得正确的等效 C++ 短语
If 1 is equal to 2 or is equal to4, then print true
如果 1 等于 2 或等于4,则打印 true
Thus it will look the following way
因此它将看起来如下
if ( 1 == 2 or 1 == 4 )
{
cout << "True";
}
else
{
cout<<"False";
}
As for the original condition
至于原始状态
1 == 2 || 4
then the compiler consideres it the following way (due to the priorities of the operators):
然后编译器按以下方式考虑它(由于运算符的优先级):
( 1 == 2 ) || 4
According to the C++ Stanbdard
根据 C++ 标准
The operators == and != both yield true or false, i.e., a result of type bool.
运算符 == 和 != 都产生真或假,即 bool 类型的结果。
So as 1 == 2 is equal to false
then you get
所以 1 == 2 等于false
那么你得到
false || 4
where 4 as it is not equal to 0 is converted to boolean true
and as result the entire condition evaluates to true
其中 4 因为它不等于 0 被转换为布尔值true
,结果整个条件评估为true
回答by NathanOliver
This not how if statements work in C++. If you want to know if something is equal to one thing or another thing then it is
这不是 if 语句在 C++ 中的工作方式。如果你想知道某事是否等于一件事或另一件事,那么它就是
if (this == this_thing || this == another_thing)
What you have
你有什么
if(1 == 2 || 4)
Gets evaluated to
被评估为
if ((1 == 2) || 4)
if (false || true)
if (true)
So the if statement will always be true.
所以 if 语句将始终为真。