使用&&和有什么区别?和||在do ... while循环中? [英] what is the difference in using && and || in the do...while loop?

查看:70
本文介绍了使用&&和有什么区别?和||在do ... while循环中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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;
}

我知道"while"中的测试条件有问题.但我无法弄清楚.

i know something is wrong with the test condition in the 'while'. But I can't figure it out.

当用户的输入既不是'n'也不是'N'时,循环应继续打印代码您在while循环中".按"n"或"N"后,程序将终止.

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.

但是对于我的代码,无论我输入什么字符,程序都将继续循环代码.但是当我更改"||"时到&&",该程序可以根据需要运行.有人可以告诉我怎么回事吗?

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?

推荐答案

这是一个常见的布尔逻辑问题. || 的意思是或",意思是只要其中之一是正确的,那么该表达式就是真实的".因此,当您将大写的'N'传递给 c!='n'||时c!='N'程序说:好吧,'N'不等于'n',因此表达式的一侧为true,因此,整个表达式都是正确的,无需检查其余的表达式."即使按下小写的'n',程序也会说嗯,'n'等于'n',但不相等到'N',因此表达式的一侧为true,因此整个表达式为true."这就是您的while循环中发生的事情.

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',因此表达式的只有一侧为true,因此表达式为false."

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."

这令人困惑,因为如果您要测试输入的字符是否等于特定值,您将使用 || (例如,我想知道'a' 'b' 'c'").

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'中的任何一个;或者换句话说,该值不能为'a' ,并且不能为'b'并且不能为'c'&& ,并且想要与该表达式相反,则需要使用 || .这是De Morgan的法律之一,我建议您仔细阅读,以免您不得不自己重新发现它们.

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' and it cannot be 'b', and it 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.

这篇关于使用&amp;&amp;和有什么区别?和||在do ... while循环中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆