In C++, why does true && true || false && false == true?

Andrew picture Andrew · Oct 31, 2010 · Viewed 35.2k times · Source

I'd like to know if someone knows the way a compiler would interpret the following code:

#include <iostream>
using namespace std;

int main() {
 cout << (true && true || false && false) << endl; // true
}

Is this true because && has a higher precedence than || or because || is a short-circuit operator (in other words, does a short circuit operator disregard all subsequent expressions, or just the next expression)?

Answer

AngelLeliel picture AngelLeliel · Oct 31, 2010

&& has a higher precedence than ||.