Basic algebra

exercise No. 275

Q:

We consider:

Code
IO.println("Result 1: " + 2.5 * 50 / 100);
IO.println("Result 2: " + 50 / 100 * 2.5);
Result
Result 1: 1.25
Result 2: 0.0

This seems to defy the rules of algebra: Why does a mere re-ordering yield a (totally) different result ? Explain both outcomes.

A:

Both operators * and / are being evaluated from left to right. In case of integer division this yields a zero net result:

2.5 * 50 / 100
2.5 * 50 / 100
  ↘  ↙     ↙
 125.0   ↙
    ↘  ↙
    1.25
50 / 100 * 2.5
50 / 100 * 2.5
  ↘  ↙     ↙
    0    ↙
    ↘  ↙
      0