Previous Topic: Precedence of Operators

Next Topic: NCL Substitution and Expressions

Parentheses to Control Evaluation Order

Although optional, it is often easier to code an expression in parentheses so that the formula being calculated is easier to read. In fact, you might have to code expressions in parentheses to ensure that your calculation is processed as you intend.

For example, the previous example shows that the result of the compound expression:

(2 + 3 * 4 ** 2)

is 50.

However, you might have meant something different and coded:

((( 2 + 3 ) * 4 ) ** 2)

In this case the expression contained in the deepest pair of parentheses is always evaluated first, which causes the calculation to proceed as follows:

( 2 + 3 ) =5
( 5 * 4 ) =20
( 20 ** 2)=400

which is obviously a completely different result from the one obtained when the same expression was coded without parentheses.

The use of parentheses to delimit the simple expressions within a compound expression is recommended for clarity of understanding.