我有一个简单的程序,其中包含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
。为什么会这样工作呢?