### 2.3.1.1 Using Parentheses

You can use parentheses to force a particular order of evaluation. When part of an expression is enclosed in parentheses, that part is evaluated first and the resulting value is used in the evaluation of the remainder of the expression.

In the following four examples, the numbers below the operators indicate a possible order of evaluation. Alternative evaluation orders are possible in the first three examples because they contain operators of equal precedence that are not enclosed in parentheses. In these cases, the compiler is free to evaluate operators of equal precedence in any order, as long as the result is the same as the result gained by the algebraic left-to-right order of evaluation.

         4 + 3 * 2 - 6/2   = 7

           ^   ^   ^  ^

           2   1   4  3

         (4 + 3) * 2 - 6/2   = 11

            ^    ^   ^  ^

            1    2   4  3

         (4 + 3 * 2 - 6)/2   = 2

            ^   ^   ^   ^

            2   1   3   4

         ((4 + 3) * 2 - 6)/2   = 4

             ^    ^   ^   ^

             1    2   3   4

As shown in the last two examples, expressions within parentheses are evaluated according to the normal order of precedence, unless you override the order by using parentheses within parentheses.

Nonessential parentheses do not affect expression evaluation, as shown in the following example:

         4 + (3 * 2) - (6/2)

However, using parentheses to specify the evaluation order is often important in high-accuracy numerical computations. In such computations, evaluation orders that are algebraically equivalent may not be computationally equivalent when processed by a computer (because of the way intermediate results are rounded off).

Parentheses can be used in argument lists to force a given argument to be treated as an expression, rather than as the address of a memory item. This usage can suppress errors if the subprogram writes to a formal argument, but the caller wants to pass a constant value.

Previous Page Next Page Table of Contents