Previous Topic Next topic Print topic


Order of Evaluation

When an expression contains more than one operator, the order in which each expression is evaluated is determined by the presence or absence of parentheses and by the priority of the expression's operators.

The following table shows the operators in order of descending priority. Operators listed on the same line have equal priority.

Operators
**
^ – (prefix unary operators)
*/
+ – (binary operators)
|| or !!
= ^=  >  <  >=  <= ^<  ^>
&
|
Note:

Open PL/I supports the use of a tilde (~) as an operator symbol equivalent to a caret (^).

Open PL/I supports the use of an exclamation point (!) as an operator symbol equivalent to a single bar (|), and a double exclamation point (!!) as equivalent to a double bar (||).

In expressions without parentheses, operators are evaluated in order of decreasing priority. For example:

B**3+C<D

is evaluated as:

((B**3)+C)<D

Operations of equal priority without parentheses are generally evaluated from left to right. For example:

A*B/C

is evaluated as:

(A*B)/C

The only exception to this rule is the prefix operators and the exponentiation operator (**), which are evaluated from right to left. For example:

A**B**C

is evaluated as:

A**(B**C)

If the expression contains parentheses, the expression within the parentheses is evaluated first according to the previous rules of priority. Its resulting value is used as a single operand. For example:

A+B*C

is evaluated as:

A+(B*C)

because * has higher priority than +; however, parentheses can be used to force another order:

(A+B)*C

If the result of an expression can be determined without evaluating all of its operands, the operands are not necessarily evaluated. A program that depends on all operands being evaluated is invalid and may produce unexpected results when compiled with optimization enabled, or when moved to another implementation of PL/I. Likewise, a program that depends on some operands not being evaluated is invalid. For example:

IF A = 0 | B/A = 5 THEN …

In this example, the Compiler can evaluate B/A before checking to see if A = 0, and the complete expression may produce a ZERODIVIDE condition. However, the order of evaluation is not guaranteed by the Compiler. In particular, if A is in fact 0, the value B/A may or may not be computed. This optimization, in which parts of a conditional expression may cause other "unnecessary" parts not to be evaluated, is called "short-circuit evaluation." Such evaluation is done at the discretion of the Compiler and its optimizer. To force A = 0 to be evaluated first, rewrite the example as follows:

IF A ^= 0 THEN
   IF B/A ^= 5 THEN 
      GOTO L;
      .
      .
      .
   L:

The remainder of this chapter describes each type of operator.

Previous Topic Next topic Print topic