Consider the operator precedence and associativity rules for the integer arithmetic operators given in the table below.
| Operator | Precedence | Associativity |
|---|---|---|
| \(+\) | Highest | Left |
| \(-\) | High | Right |
| \(*\) | Medium | Right |
| \(/\) | Low | Right |
The value of the expression \(3 + 1 + 5 * 2 \mathbin{/} 7 + 2 - 4 - 7 - 6 \mathbin{/} 2\) as per the above rules is __________
Answer
6
Explanation
The operator with the lowest precedence is applied last, so the expression splits first at \(/\). As \(/\) is right-associative, it reads \(A \mathbin{/} (B \mathbin{/} C)\) with \(A = 3 + 1 + 5 * 2\), \(B = 7 + 2 - 4 - 7 - 6\) and \(C = 2\).
- In \(A\), \(+\) binds tighter than \(*\): \(A = (3 + 1 + 5) * 2 = 18\).
- In \(B\), \(+\) binds first, giving \(9 - 4 - 7 - 6\); right-associative \(-\) groups this as \(9 - (4 - (7 - 6)) = 9 - 3 = 6\).
- The value is \(18 \mathbin{/} (6 \mathbin{/} 2) = 18 \mathbin{/} 3 = 6\), and every division is exact.
The usual C rules would give \(-7\) instead.
Answer: 6