We get into unnecessary coding arguments at my work all-the-time. Today I asked if conditional AND (&&) or OR (||) had higher precedence. One of my coworkers insisted that they had the same precedence, I had doubts, so I looked it up.
According to MSDN AND (&&) has higher precedence than OR (||). But, can you prove it to a skeptical coworker?
bool result = false || true && false; // --> false // is the same result as bool result = (false || true) && false; // --> false // even though I know that the first statement is evaluated as bool result = false || (true && false); // --> false
So my question is how do you prove with code that AND (&&) has a higher precedence that OR (||)? If your answer is it doesn't matter, then why is it built that way in the language?
Change the first false by true. I know it seems stupid to have (true || true) but it proves your point.
bool result = true || true && false; // --> true result = (true || true) && false; // --> false result = true || (true && false); // --> true