简体   繁体   English

循环中与OR逻辑运算符的混淆

[英]Confusion with the OR logical Operator in a Loop

A bit earlier on an IRC channel, someone asked a question about his code - essentially, his program was running on an infinite loop: 早在IRC频道上,有人问了有关他的代码的问题-本质上说,他的程序在无限循环中运行:

#include <iostream>
using namespace std;

int main()
{
cout << "This program adds one to your input." << endl;

char choice = 'n';
int num = 0;

while (choice != 'y' || choice != 'Y')
{

    cout << "Enter number: ";

    cin >> num;
    num++;
    cout << "Your number plus 1 is: " << num << endl;
    cout << endl << "Continue/Quit? Type 'Y' to quit, any other key to continue: ";

    cin >> choice;
}

cout << "By choosing 'Y', you exit the loop." << endl;

return 0;

}

Paying attention to the loop header, it seems that the loop should be working perfectly fine, but whenever it comes time for me to enter Y or y in order to exit, the loop keeps running. 注意循环头,看起来循环应该工作得很好,但是每当我需要输入Y或y退出时,循环就会继续运行。 Considering that the while loop won't evaluate the expression to the right if the one on the left is true, this makes it especially confusing. 考虑到如果左侧的表达式为true,则while循环不会评估右侧的表达式,因此这尤其令人困惑。 But in any case, even if I input Y or y the loop keeps running! 但是无论如何,即使我输入Y或y,循环也会继续运行! I'd like a somewhat in-depth explanation of why this happens, I've been trying to reason it out, look back in my textbook etc. but I can't seem to understand why... I've changed the OR into an AND, but what makes OR so bad and causes it to malfunction? 我想对发生这种情况的原因进行更深入的解释,我一直试图将其解释出来,回头看课本等。但是我似乎不明白为什么...我改变了OR进入“与”门,但是什么使“或”门如此糟糕并导致其故障呢?

Thanks all. 谢谢大家

Condition (choice != 'y' || choice != 'Y') is always true, so loop will run indefinetely. 条件(choice != 'y' || choice != 'Y')始终为true,因此循环将不确定地运行。 If choice == 'y', then you get (false || true) == true; 如果选择=='y',则得到(false || true)== true; if choice == 'Y', then you get (true || false) == true. 如果选择=='Y',则得到(true || false)== true。 You need to use while(choice != 'y' && choice != 'Y') instead, in this case you exit loop only if you get 'y' or 'Y', otherwise you get (true && true) and continue. 您需要使用while(choice != 'y' && choice != 'Y')来代替,在这种情况下,仅当您获得'y'或'Y'时才退出循环,否则获得(true && true)并继续。

The OR operator between many statements returns true if at least 1 of the statements is true, no matter if the others are false or true. 如果至少一条语句为true,则许多语句之间的OR运算符将返回true,无论其他语句为false还是true。 In your case, choice != 'y' || choice != 'Y' 在您的情况下, choice != 'y' || choice != 'Y' choice != 'y' || choice != 'Y' will be evaluated like this: choice != 'y' || choice != 'Y'将像这样评估:

First statement is true: Execute while loop. 第一句话是正确的:执行while循环。

First statement is false: Check if the second statement is true. 第一条陈述为假:检查第二条陈述是否为真。

Second statement is true: Execute while loop. 第二句话是正确的:执行while循环。

Second statement is false: don't execute while loop. 第二句话是错误的:不要在while循环中执行。

Specifically, when the compiler arrives at choice != 'y' , if choice == 'Y' , then it will still execute, because choice != 'y' is true, in fact choice is equals to Y , so it's different from y . 具体来说,当编译器到达choice != 'y' ,如果choice == 'Y' ,则编译器仍将执行,因为choice != 'y'为true,实际上选择等于Y ,因此与y

If, on the other hand, choice is equals to y , then it will check if the second statement is true, but we know that choice is equals to y , so choice is different from Y , and so on... 另一方面,如果choice等于y ,则它将检查第二条语句是否为true,但我们知道choice等于y ,所以choice不同于Y ,依此类推...

You make a mistake,you should change "||" 您输入有误,请更改“ ||” to "&&" 至 ”&&”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM