【发布时间】:2019-11-23 19:04:04
【问题描述】:
我有一个简单的程序,它有 1 个 AND 和多个 OR 运算符,如下所示:
#include <iostream>
using namespace std;
int main()
{
bool a = true;
bool b = true;
bool c = true;
bool d = true;
if (!a && b || c || d)
cout << "run";
else
cout << "pass";
return 0;
}
我希望程序会输出pass,因为我将 a 声明为 true。但是,如果你运行程序,它会给出输出:run
如果我通过在
中添加括号来更改 if 语句行if (!a && (b || c || d))
cout << "run";
else
cout << "pass";
它将给出预期的输出pass。为什么会这样?
【问题讨论】:
标签: c++ if-statement logical-operators