An expression is formed from operands, operators, and parentheses. Evaluation of an expression produces a value with a type, type parameters (kind and, if CHARACTER, length), and a shape. Some examples of valid Fortran expressions are:
5
n
(n+1)*y
"to be" // ' or not to be' // text(1:23)
(-b + (b**2-4*a*c)**.5) / (2*a)
b%a - a(1:1000:10)
sin(a) .le. .5
l .my_binary_operator. r + .my_unary_operator. m
The last example uses defined operations (see Defined Operations).
All array-valued operands in an expression must have the same shape. A scalar is conformable with an array of any shape. Array-valued expressions are evaluated element-by-element for corresponding elements in each array and a scalar in the same expression is treated like an array where all elements have the value of the scalar. For example, the expression
a(2:4) + b(1:3) + 5
would perform
a(2) + b(1) + 5
a(3) + b(2) + 5
a(4) + b(3) + 5
Expressions are evaluated according to the rules of operator precedence, described below. If there are multiple contiguous operations of the same precedence, subtraction and division are evaluated from left to right, exponentiation is evaluated from right to left, and other operations can be evaluated either way, depending on how the compiler optimizes the expression. Parentheses can be used to enforce a particular order of evaluation.
A specification expression is a scalar INTEGER expression that can be evaluated on entry to the program unit at the time of execution. An initialization expression is an expression that can be evaluated at compile time.
The intrinsic operators, in descending order of precedence are:
|
Operator |
Represents |
Operands |
|---|---|---|
|
** |
exponentiation |
two numeric |
|
* and / |
multiplication and division |
two numeric |
|
+ and - |
unary addition and subtraction |
one numeric |
|
+ and - |
binary addition and subtraction |
two numeric |
|
// |
concatenation |
two CHARACTER |
|
.EQ. and == .NE. and /=
.LT. and < .LE. and <= .GT. and > .GE. and >= |
equal to not equal to
less than less than or equal to greater than greater than or equal to |
two numeric or two CHARACTER -----------
two non-COMPLEX numeric or two CHARACTER |
|
.NOT. |
logical negation |
one LOGICAL |
|
.AND. |
logical conjunction |
two LOGICAL |
|
.OR. |
logical inclusive disjunction |
two LOGICAL |
|
.EQV. and .NEQV. |
logical equivalence and non-equivalence |
two LOGICAL |
Note: all operators within a given cell in the table are of equal precedence
If an operation is performed on operands of the same type, the result is of that type and has the greater of the two kind type parameters.
If an operation is performed on numeric operands of different types, the result is of the higher type, where COMPLEX is higher than REAL and REAL is higher than INTEGER.
If an operation is performed on numeric or LOGICAL operands of the same type but different kind, the result has the kind of the operand offering the greater precision.
The result of a concatenation operation has a length that is the sum of the lengths of the operands.
The result of a division operation between two INTEGER operands is the integer closest to the mathematical quotient and between zero and the mathematical quotient, inclusive. For example, 7/5 evaluates to 1 and -7/5 evaluates to -1.