Given
int x=1,y=2,z;
Could you explain why the result for:
x && y || z
is 1?
x && y = 1
x && y || z = 1
x && y || z
is equivalent to
(x && y) || z
if x=1
and y=2
then x&&y
is 1 && 2
which is true && true
which is true
.
true || z
is always true
. z
isn't even evaluated
x && y || z
=> (x && y) || z
=> 1 || z
=> 1
(bool)1 = true
(bool)2 = true
Uninitialized int refers to data that was saved in memory, where it is placed on stack... and it rarely is 0x00000000
, and even if it was, true || false = true
.
The &&
operator has higher precedence than the ||
operator. See, e.g., this operators precedence table, numbers 13 and 14.
Your example evaluates as (x && y) || z
. Thanks to the short circuiting rule, z
is never evaluated because the result of x && y
is already true
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With