Search code examples
c++operatorsoperator-precedencelogical-orlogical-and

Operator precedence and evaluation order


I can't understand output of this program:

#include<iostream>
using namespace std;
int main()
{
    int x = 1 , y = 1, z = 1;
    cout << ( ++x || ++y && ++z ) << endl; //outputs 1;
    cout << x << " " << y << " " << z ;  //x = 2 , y = 1 , z = 1;
    return 0;
}

Output:

1
2 1 1

If || is evaluated first then this output is fine, however this article says that && has a higher precedence than ||, and thus it must be evaluated first. If this is the case then according to me output should be:

1
1 2 2

as ++y && ++z would evaluate to true and thus ++x won't be evaluated.


Solution

  • "Precedence" affects grouping, not order, and means that if there could be any ambiguity regarding which operator an operand "belongs to", the operator with the higher precedence gets first dibs on it.

    Since there are two binary operators involved, there are two ways you can read the expression.
    As trees, these would be:

        and
        /\
       or ++z       [(++x || ++y) && ++z]
      / \
    ++x ++y 
    
    
       or
       /\
    ++x  and       [++x || (++y && ++z)]
         / \
      ++y ++z
    

    The precedence rules determine that the latter tree is chosen in C++, since the middle operand, ++y, is grouped with &&, not with ||.

    The "short-circuiting" of these operators means that evaluation must start at the leftmost leaf (each operator must first evaluate its left leg, then its right if it needs to).
    Thus, ++x is evaluated first, and || only continues with its right leg if ++x is zero, which it isn't.

    (As can be seen from the wonderful and artistic diagrams, ++x must be evaluated first regardless of the relative precedence of && and ||.)