#515 – Binary Operators Are Left-Associative

When an expression contains more than one binary operators, where the operators are identical or have the same precedence, the operators are left-assocative.  This means that the expression is evaluated from left to right.

For example, the result of the expression shown below is 5, rather than 20.  80 is divided by 8 to get an intermediate result of 10.  10 is then divided by 2 to get a result of 5.

            double result = 80 / 8 / 2;

This means that the above expression is equivalent to:

            double result = (80 / 8) / 2;

If you want to force the division of the 2nd and 3rd operands to happen first, you could use parentheses around them:

            // result = 20
            double result = 80 / (8 / 2);