dimanche 14 juillet 2019

Why adding bracket in If statements with Multiple AND OR gives different result?

I have simple program that has 1 AND and multiple OR operators as below :

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

I expect the program will output pass because I declare a as true. But, if you run the program it will give output : run

If I change the if statement line by adding bracket to

if (!a && (b || c || d))
        cout << "run";
    else
        cout << "pass";

It will give the expected output pass. Why does it work that way?

Aucun commentaire:

Enregistrer un commentaire