FORTRAN Reference Manual
Expressions
FORTRAN Reference Manual—528615-001
3-3
Evaluation of Arithmetic Expressions
Evaluation of Arithmetic Expressions
The hierarchy of arithmetic operators determines the order in which the operands are
combined:
** <-- Highest
* and /
+ and - <-- Lowest
For example, in the following expression, the exponentiation operator takes
precedence over the negation operator:
-A ** 5 <-- Evaluated as -(A ** 5)
You can use parentheses to override the normal sequence of evaluation. Within a
compound expression, FORTRAN evaluates an expression enclosed in parentheses
first. If you use nested parentheses, FORTRAN evaluates the deepest level expression
first. Within parentheses, evaluation proceeds in the normal sequence. Parenthetical
expressions must be balanced: you must match every left parenthesis with a
corresponding right parenthesis. FORTRAN uses the following rules to evaluate
expressions whose value depends upon precedence of operators:
•
If adjacent operators have different precedence, the operator with higher
precedence is evaluated first.
•
Adjacent operators that have the same precedence are evaluated according to
their associativity: right-to-left for the exponentiation operator, left-to-right for all
others.
The preceding rules specify the order in which operators are executed, but not the
order in which an operator’s operands are evaluated. Thus, for example, the
expression A + B + C is evaluated as (A + B) + C, but the compiler could evaluate C
before it evaluates (A + B). If evaluating an operand can have side effects, for example
by calling a function, the order in which the operands are evaluated can be important.
The following examples illustrate how HP FORTRAN evaluates expressions. The
shaded portion at each step, consisting of an operator and two operands, shows the
operator evaluated at that step.
•
Example 1
5 + - 6
*
23
5 + 3 * 2 - 6
5 + - 66
11
- 6
5
VST0301.vsd










