I'm struggling with understanding why the following returns this value. Any help would be appreciated.
int ans = 10, v1 = 5, v2 = 7, v3 = 18;
ans += v1 + 10 * (v2-- / 5) + v3 / v2;
Console.WriteLine(ans);// prints 28
My thinking would be brackets first, division, multiplication then addition. So the steps would be: v1 + 10 * (v2-- / 5) + v3 / v2
Therefore, (ans += 12) = 22?
v2-- / 5)= 1.4
and there is your problem. Integer division will never return a non integer value.
1/2
equals 0
, not 0.5
and 7/5
equals 1
, not 1.4
.