Originally posted by Anupreet Arora:
but i am unable to understand why
does'nt && have higher precedence than ||
should'nt the evaluation be :
((a = true) || ((b = true) && (c = true)));
Yes, it is. Try it, it returns "true false false". It makes sense to me, but I'll admit I'm hard pressed to come up with a clear explaination of why. Basically, order of operations does not override the rule that expressions are evaluated left to right.
What is clear from the above expression (especially with parens included) is that the right hand side of the || operator is ((b=true)&&(c=true)). The right hand side of the conditional or operator (||) does not get evaluated if the left hand side is true, which it is. Therefore no part of
((b=true)&&(c=true)) gets evaluated.
Originally posted by Anupreet Arora:
Secondly, how come an assignment expression (a=true) return true itself?
(a=true) is itself an assignment expression of type boolean, which evaluates to true, and has the side effect that a gets assigned to true. When I see this (a=true) in an expression I mentally replace it with just true and make a mental note that a now also has the value true. Note that the parens are necessary here as assignment (=) has the lowest precedence of all operators.