具有逻辑或的IF语句 [英] IF statement with logical OR
问题描述
if(1 == 2 || 4)
{
cout<<"True";
}
else
{
cout<<"False";
}
这就是我阅读以上内容的方式.如果1等于2或4,则打印true.否则,打印false.当执行此命令时,将打印true.显然我在这里误会了一些东西. 1不等于2或4.难道不是假的吗?
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?
推荐答案
是的,我犯了同样的错误.
Yeah, I've made the same mistake.
再读一遍句子:
Read the sentence again:
如果1等于2或4,则打印true.
If 1 is equal to 2 or 4, then print true.
"2"和"4"均表示如果1等于[...]".这意味着该句子只是
The "2" and "4" both refer to the "If 1 is equal to [...]." That means, the sentence is just an abbreviation of
如果1等于2或 1等于 4,则打印true.
If 1 is equal to 2 or 1 is equal to 4, then print true.
这导致我们进入if
-条款
if (1 == 2 || 1 == 4)
相反.
1 == 2 || 4
为true,因为(1 == 2) == false
与4 == true
进行或运算,得出true(false或true = true).
1 == 2 || 4
is true because (1 == 2) == false
ORed with 4 == true
, yields true (false OR true = true).
这篇关于具有逻辑或的IF语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!