1

So I have this program that returns "result: true"

if (true == false != true) {
    cout << "result: true";
}
else {
    cout << "result: false";
}

even if we flip the comparison operators inside of the if-statement, the compiler still evaluates the expression to be true

if (true != false == true)

My question is:

  1. How does the compiler actually evaluates the expression?
  2. and to which comparison operator out of the two present inside of the if-statement, preference is given?
yashsh
  • 81
  • 11
  • @alfC can you please kindly be more specific in your comments as to where my question lacks proper explanation. – yashsh Sep 08 '21 at 05:17
  • I mean that it is useless to know because even if it compiles you don't want to have that code because it is impossible to understand. And more to the point, `(true != false == true)` is `true` (in C++) and if that is `true` then it doesn't mean what you really want it to mean in the real world. In other words, abusing the associativity of (in)equality is in general a bad idea in C++. – alfC Sep 08 '21 at 05:27
  • @alfC, thanks for the clarification. This was actually an assignment problem i had about evaluating expression with multiple operators and i know has no real world explanation. I just wanted to know how exactly the compiler evaluates this kind of expressions. – yashsh Sep 08 '21 at 05:45
  • 1
    My question is how do **you** evaluate that expression? If the answer is "I don't know" (which would be my answer), then don't write it. – Pete Becker Sep 08 '21 at 13:17

2 Answers2

4

The answer to both of your questions is operator precedence. The == and != operators get the same precedence, meaning they will be evaluated in the order given.

So in true == false != true, is evaluated as (true == false) != true first statement true==false being false, the full statement now becomes false!=true which evaluates to true

Similarly, 2nd statement true != false == true becomes (true != false) == true which evaluates to true at the end


EDIT:

After reading @Pete's comment, I did some more reading. Apparently there is an associativity property related to these kinds of situations

From https://en.cppreference.com/w/cpp/language/operator_precedence

Operators that have the same precedence are bound to their arguments in the direction of their associativity. For example, the expression a = b = c is parsed as a = (b = c), and not as (a = b) = c because of right-to-left associativity of assignment, but a + b - c is parsed (a + b) - c and not a + (b - c) because of left-to-right associativity of addition and subtraction.

Teshan Shanuka J
  • 1,448
  • 2
  • 17
  • 31
  • thank you for the explanation. I think my query is resolved. – yashsh Sep 08 '21 at 05:27
  • 3
    No, this leaves out the actual answer, which is **grouping**. Some operators group left to right; some group right to left. For example, addition groups left to right, so `a + b + c` is equivalent to `(a + b) + c`. Assignment groups right to left, so `a = b = c` is equivalent to `a = (b = c)`. – Pete Becker Sep 08 '21 at 13:22
  • @PeteBecker Interesting. Thanks, I did not know about that concept. Is there any source to read more about this? – Teshan Shanuka J Sep 08 '21 at 17:05
0
  1. For this particular case, the compiler evaluates the expression from left to right, as "==" and "!=" are of equal precedence.
  2. As mentioned above, these two operators have the same precedence. For more information check out C++ Operator Precedence
  • Since the two operators have the same precedence, the answer cannot be precedence. In fact, it's **grouping**, which that link refers to as "associativity". – Pete Becker Sep 08 '21 at 13:25