C++ 使用 && 和 || 有什么区别 在 do...while 循环中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/647413/
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
what is the difference in using && and || in the do...while loop?
提问by Jim H.
#include<iostream>
using namespace std;
int main()
{
char again;
do
{
cout<<"you are in the while loop";
cout<<"do you want to continue looping?";
cin>>again;
} while (again != 'n' || again != 'N');
system("pause");
return 0;
}
i know something is wrong with the test condition in the 'while'. But I can't figure it out.
我知道“while”中的测试条件有问题。但我想不通。
when the input of the user is neither 'n' nor 'N', the loop should keep on printing the code "you are in the while loop". Once 'n' or 'N' is pressed, the program will be terminated.
当用户的输入既不是 'n' 也不是 'N' 时,循环应该继续打印代码“you are in the while loop”。一旦按下“n”或“N”,程序将终止。
However for my code, program will keep on looping the code regardless what character i enter. But when i change the '||' to '&&', the program can ran as desired. Anyone can tell me what is going on?
但是对于我的代码,无论我输入什么字符,程序都会继续循环代码。但是当我改变'||' 到'&&',程序可以根据需要运行。谁能告诉我这是怎么回事?
回答by Jon Skeet
Yes: || is "or" and && is "and".
是:|| 是“或”,&& 是“和”。
Everycharacter is either"not 'n'" or "not 'N'" because it can't possibly be both'n' and 'N' simultaneously.
每个字符要么是“not 'n'”,要么是“not 'N'”,因为它不可能同时是'n' 和 'N'。
Another (probably simpler to read) way of writing the condition would be:
另一种(可能更容易阅读)写条件的方法是:
!(again == 'n' || again == 'N')
which means "the opposite of (again is either'n' or 'N')".
该装置“的相反(再次是任一‘n’或‘N’)”。
回答by Max Lybbert
This is a common boolean logic question. ||
means "or," which means "as long as one side of this is true, then the expression is true." So when you pass an uppercase 'N'
to c != 'n' || c != 'N'
the program says "well, 'N'
is not equal to 'n'
, therefore one side of the expression is true, therefore the whole expression is true and there is no need to check the rest of the expression." Even when you press lowercase 'n'
, the program says "well, 'n'
is equal to 'n'
, but it's not equal to 'N'
, therefore one side of the expression is true, therefore the whole expression is true." This is what is happening in your while loop.
这是一个常见的布尔逻辑问题。 ||
意思是“或”,意思是“只要这个的一侧是真的,那么这个表达式就是真的”。因此,当您将大写字母传递'N'
给c != 'n' || c != 'N'
程序时会说“好吧,'N'
不等于'n'
,因此表达式的一侧为真,因此整个表达式为真,无需检查表达式的其余部分。” 即使按小写'n'
,程序也会说“好吧,'n'
等于'n'
,但不等于'N'
,因此表达式的一侧为真,因此整个表达式为真。” 这就是你的 while 循环中发生的事情。
On the other hand, &&
means "and" which means "both sides of the expression must be true"; when you pass an uppercase 'N'
to c != 'n' && c != 'N'
the program thinks "'N'
is not equal to 'n'
, but it is equal to 'N'
, therefore only one side of the expression is true, therefore the expression is false."
另一方面,&&
表示“和”,表示“表达式的两边都必须为真”;当您将大写字母传递'N'
给c != 'n' && c != 'N'
程序时,会认为“'N'
不等于'n'
,但等于'N'
,因此表达式只有一侧为真,因此表达式为假”。
This gets confusing because if you were testing to see if the characters entered were equal to particular values you would use ||
(e.g., "I want to know if 'a'
or'b'
or'c'
was entered").
这会令人困惑,因为如果您正在测试输入的字符是否等于您将使用的特定值||
(例如,“我想知道是否输入了'a'
或'b'
或'c'
已输入”)。
Basically, when you would use ||
for a particular expression, and you want the opposite of that expression then you need to change to &&
(e.g., I want none of 'a'
, 'b'
or 'c'
; or to put it another way, the value cannot be 'a'
andit cannot be 'b'
, andit cannot be 'c'
"). Likewise, if you would use &&
for a particular expression, and you want the opposite of that expression then you need to use ||
. This is one of De Morgan's laws, which I would recommend you read up on so you can avoid having to rediscover each of them on your own.
基本上,当你使用||
特定的表情,和你想要的表达相反,那么你需要改变&&
(例如,我想都没有'a'
,'b'
或者'c'
,或者换一种说法,该值不能'a'
和它不能'b'
,并且它不能是'c'
“)。同样,如果你要使用&&
一个特定的表达式,并且你想要那个表达式的反面,那么你需要使用||
。这是德摩根定律之一,我建议你阅读它您可以避免自己重新发现它们中的每一个。
回答by Chaowlert Chaisrichalermpol
It is the boolean algebra called "De Morgan's laws".
它是布尔代数,称为“德摩根定律”。
Not (P And Q) = (Not P) Or (Not Q)
Not (P Or Q) = (Not P) And (Not Q)
In your case, you want users not to enter 'n'
or 'N'
, it can be translate in to this logic.
在您的情况下,您希望用户不要输入'n'
或'N'
,它可以转换为此逻辑。
!(again == 'n' || again == 'N')
When applying De Morgan's laws, it will be
在应用德摩根定律时,它将是
(again != 'n' && again != 'N')
For more info, you might want to read Propositional logic
有关更多信息,您可能需要阅读命题逻辑
回答by Daniel Earwicker
Although the original poster is happy now, I didn't see this in the other answers:
虽然原来的海报现在很开心,但我在其他答案中没有看到这一点:
(true && true ) == true
(true && false) == false
(false && true ) == false
(false && false) == false
(true || true ) == true
(true || false) == true
(false || true ) == true
(false || false) == false
!true == false
!false == true
That's everything!
这就是一切!
回答by Ameraradi
I understand your problem well,and here is the explanation:
我很理解你的问题,这里是解释:
The do-while loop is an exit-condition loop. This means that the body of the loop is always executed first. Then, the test condition is evaluated. If the test condition is TRUE, the program executes the body of the loop again. If the test condition is FALSE, the loop terminates and program execution continues with the statement following the while.
do-while 循环是一个退出条件循环。这意味着循环体总是首先执行。然后,评估测试条件。如果测试条件为 TRUE,程序将再次执行循环体。如果测试条件为 FALSE,则循环终止,程序继续执行 while 之后的语句。
in your code,when you press'n' or'N',your test condition will be always one true and one false, so when you use || you will satisfy the test condition (true||false=true) ,so the program will execute the body of the loop again. but when you use && ,this will gives (true && false =false),the test condition is not statisfied anymore,so the program will not execute the body of the loop again.
在您的代码中,当您按“n”或“N”时,您的测试条件将始终为一真一假,因此当您使用 || 时 您将满足测试条件 (true||false=true) ,因此程序将再次执行循环体。但是当你使用 && 时,这将给出 (true && false =false),测试条件不再稳定,因此程序不会再次执行循环体。
I hope that's helpful.....enjoy programing! Ameraradi
我希望这有帮助.....享受编程!亚美拉迪
回答by Jim H.
&& is a logical AND. || is a logical OR.
&& 是逻辑与。|| 是逻辑 OR。
(also, & is a bitwise AND, and | is a bitwise OR.)
(另外,& 是按位与,而 | 是按位或。)
回答by Pete
you might want to try while(!(again == 'n' || again == 'N'))
你可能想尝试 while(!(again == 'n' || again == 'N'))