Different types of operations can be combined within the same operational expression. Any combination can be used.
For example:
declare Result bit(3),
A fixed decimal(1),
B fixed binary (3),
C character(2), D bit(4);
Result = A + B < C & D;
Each operation within the expression is evaluated according to the rules for that kind of operation, with necessary data conversions taking place before the operation is performed, as follows:
The expression in this example is evaluated operation-by-operation, from left to right. The order of evaluation, however, depends upon the priority of the operators appearing in the expression.
The priority of the operators in the evaluation of expressions is shown in Table 21.
| Priority | Operator | Type of Operation | Remarks |
|---|---|---|---|
| 1 | ** | Arithmetic | Result is in coded arithmetic form |
| prefix +, - | Arithmetic | No conversion is required if operand is in coded arithmetic form | |
| Operand is converted to FIXED DECIMAL if it is a CHARACTER string or numeric character (PICTURE) representation of a fixed-point decimal number | |||
| Operand is converted to FLOAT DECIMAL if it is a numeric character (PICTURE) representation of a floating-point decimal number | |||
| Operand is converted to FIXED BINARY if it is a BIT string | |||
| prefix ¬ | Bit string | All non-BIT data converted to BIT | |
| 2 | *, / | Arithmetic | Result is in coded arithmetic form |
| 3 | infix +, - | Arithmetic | Result is in coded arithmetic form |
| 4 | || | Concatenation | Refer to Results under RULES(ANS) and Results under RULES(IBM) |
| 5 | <, ¬<, <=, =, ¬=, >=, >, ¬> | Comparison | Result is always either '1'B or '0'B |
| 6 | & | Bit string | All non-BIT data converted to BIT |
| 7 | | | Bit string | All non-BIT data converted to BIT |
| infix ¬ | Bit string | All non-BIT data converted to BIT |
The order of evaluation of the expression
A + B < C & D
is the same as if the elements of the expression were parenthesized as
(((A + B) < C) & D)
The order of evaluation (and, consequently, the result) of an expression can be changed through the use of parentheses. Expressions enclosed in parentheses are evaluated first, to a single value, before they are considered in relation to surrounding operators.
The above expression, for example, might be changed as follows:
(A + B) < (C & D)
The value of A converts to fixed-point binary, and the addition is performed, yielding a fixed-point binary result (result_1). The value of C converts to a bit string (if valid for such conversion) and the and operation is performed. At this point, the expression is reduced to:
Result_1 < Result_2
Result_2 is converted to binary, and the algebraic comparison is performed, yielding a bit string of length 1 for the entire expression.
The priority of operators is defined only within operands (or sub-operands). Consider the following example:
A + (B < C) & (D || E ** F)
In this case, PL/I specifies only that the exponentiation occurs before the concatenation. It does not specify the order of the evaluation of (D||E ** F) in relation to the evaluation of the other operand (A + (B < C)).
Any operational expression (except a prefix expression) must eventually be reduced to a single infix operation. The operands and operator of that operation determine the attributes of the result of the entire expression. In the following example, the & operator is the operator of the final infix operation.
A + B < C & D
The result of the evaluation is a bit string of length 4.
In the next example, because of the use of parentheses, the operator of the final infix operation is the comparison operator:
(A + B) < (C & D)
The evaluation yields a bit string of length 1.