expression - Java modulus operator - why is the result unexpected? -
i understand in modulus 17/12 = 5
.
why 4+17 % 2-1
value 4
, , (4+17) % 2-1
value 0
?
operator precedence. %
evaluated first, so
4 + 17 % 2 - 1
is equivalent to
4 + (17 % 2) - 1
17%2 == 1
yields 4+1-1
equals 4
when place brackets there, change order of evaluation:
(4+17) % 2 - 1
is equivalent to
21 % 2 - 1
which again, because of %
having higher precendence -
, yields
1 - 1
which 0
Comments
Post a Comment