The order of precedence is the order in which operators (relational, logical, mathematical, and special function) are evaluated.
Operators that have a higher precedence are evaluated before operators that have a lower precedence.
If all the operators in the formula have equivalent precedence levels, then the operators are evaluated from left to right.
The following example illustrates how precedence levels are applied to mathematical operators.
Example 1
10 - 5 + 2 * 3 =
x
This equation is solved as follows:
- Multiplication has the highest precedence level in this equation. The first step in evaluating the equation is as follows:
2 * 3 = 6
After the first level of operators has been evaluated, the equation appears as follows:
10 - 5 + 6 =
x
- Since the remaining operators have the same precedence level, operators are now evaluated from left to right. The last step in evaluating the equation is as follows:
10 - 5 = 5
5 + 6 = 11
- The solution to this equation is 11.
See Table 1-5 for a complete list of precedence levels for PPCL operators.
Statements containing relational and logical operators are also assigned a precedence level.
Note that in problems comparing values using relational operators (.EQ. and .NE.), the result of the comparison can change the output of the statement.
Example 2
If (value1 .EQ. value2 .AND. value1 .NE. value3) then...
This equation is solved as follows:
- The relational operators (.EQ. and .NE.) have the highest precedence level in the formula. Since these two operators have the same precedence level, they are evaluated from left to right.
In this example, assume that both comparisons (condition1 and condition2) are true.
value1 .EQ. value2 (condition1 = true)
value1 .NE. value3 (condition2 = true)
- After the first level of operators has been evaluated, the equation now looks like the following:
If (condition1 .AND. condition2) then...
- The logical operator .AND. is then evaluated.
Since .AND. requires both conditions to be true, the solution to this problem will also be true.