Precedence  - Parentheses


When evaluating expressions certain operators have a higher precedence than others and hence are evaluated first. However it is possible to manipulate the order in which functions and operators are evaluated through the use of parentheses or round brackets. For example, the following expression part would evaluate to 18:

10 + 2 * 4

Because of the operator precedence the multiplication operator would be evaluated first, i.e. 2*4 = 8, followed by the addition operator, i.e. 10+8 = 18. The next expression part, though, would evaluate to 48:

(10 + 2) * 4

In this case, the operator precedence has been over-ridden through the use of the parentheses, and as a result the bracketed addition operation is evaluated first, i.e. (10 + 2) = 12, followed by the multiplication operator, i.e. 12 * 4 = 48. Effectively, any bracketed expression is treated as an operand, and so is evaluated before any further operations can be performed using it.

Parentheses can also be nested, as in the following expression part which evaluates to 10:

(10 * (4 + 2) - 20) / 4

Here, the 4 + 2 is evaluated first to give 6, then multiplied by 10 to give 60, after which 20 is subtracted giving 40, and finally 40 is divided by 4 to give 10.