logical operators


int x,y,z;
x=y=z=1;
++x||++y&&++z;
cout<<x<<y<<z;--->x=2,y=1,z=1

i have tried this puzzle in gcc compiler. I didn't get idea behind this expression.could any one tell how it will going to evaluate?
This takes advantage of a language feature known as "short circuiting".

With the below code:

 
a || b


'a' is evaluated first. If it results to nonzero (true), then the compiler knows that the || operator will also result in true, so it has no need to evaluate 'b'.

Similarly.. a && b, if 'a' results in zero (false), then the compiler knows that the && will also result in false, so there's no need to evaluate 'b'.

This is often (ab)used to check to see if a pointer is null before dereferencing it:

1
2
3
4
5
MyClass* ptr = /*something, possibly null*/

if(ptr && ptr->something)  // if the pointer is non-null, then dereference it and check 'something'
{
}


What's happening in your example is this:

 
++x||++y&&++z;


1) ++x is being evaluated first, which results in 2 (nonzero, true), so the right side of the || operator is skipped

2) || has a lower operator precedent than &&, so ++y&&++z is all considered the right side of the expression... just as if you had said:
++x || (++y && ++z), therefore, all of it is skipped.

End Result: only x was incremented. y and z increments were short circuited.



Compare that to (++x || ++y) && ++z;, which will increment both x and z, because the || short circuit will now only remove the y increment.
Last edited on
ya good,i got clear idea about evaluation,but why logical OR getting higher priority than logical AND and according to precedence rule && higher precedence than ||,does it follow operator precedence?
Last edited on
but why logical OR getting higher priority than logical AND


It's not. It's getting lower priority. Having higher priority doesn't necessarily mean the compiler has to evaluate it first, it just determines what is considered the left and right hand side of the operators.

For example:

x + y * z is the same as x + (y * z) because * has higher priority. If + had higher priority, it would be the same as (x + y) * z

Same deal with the || operator:
x || y && z is the same as x || (y && z) because && has a higher priority.

The difference here is that + needs to evaluate both the left and right hand side of the expression before it can evaluate. This means that y*z does have to evaluate first, because its result will be used as the right side of +.

However, || only needs to evaluate the left side.. and if the left side is true, the entire right side can be dropped and never needs to be evaluated. So here, || is evaluated first, despite having lower priority.

But that's just due to the way the expression is arranged. For example: y && z || x would evaluate y && z first, and if it is true, then evaluation of x would be skipped.
Last edited on
ya thank you... now i got clear idea about these precedence because the way of you have explained. then i will meet you some other doubts...bye
Topic archived. No new replies allowed.