An expression represents either a data reference or a computation,
and is formed from operators, operands, and parentheses. The
result of an expression is either a scalar value or an array of
scalar values.
If the value of an expression is of intrinsic type, it has a kind
type parameter. (If the value is of intrinsic type CHARACTER, it
also has a length parameter.) If the value of an expression is of
derived type, it has no kind type parameter.
An operand is a scalar or array. An operator can be either
intrinsic or defined. An intrinsic operator is known to the
compiler and is always available to any program unit. A defined
operator is described explicitly by a user in a function subprogram
and is available to each program unit that uses the subprogram.
The simplest form of an expression (a primary) can be any of the
following:
o A constant; for example, 4.2
o A subobject of a constant; for example, 'LMNOP'(2:4)
o A variable; for example, VAR1
o A structure constructor; for example, EMPLOYEE(3472, "JOHN
DOE")
o An array constructor; for example, (/12.0,16.0/)
o A function reference; for example, COS(X)
o Another expression in parentheses; for example, (I+5)
Any variable or function reference used as an operand in an
expression must be defined at the time the reference is executed.
If the operand is a pointer, it must be associated with a target
object that is defined. An integer operand must be defined with an
integer value rather than a statement label value. All of the
characters in a character data object reference must be defined.
When a reference to an array or an array section is made, all of
the selected elements must be defined. When a structure is
referenced, all of the components must be defined.
In an expression that has intrinsic operators with an array as an
operand, the operation is performed on each element of the array.
In expressions with more than one array operand, the arrays must be
conformable (they must have the same shape). The operation is
applied to corresponding elements of the arrays, and the result is
an array of the same shape (the same rank and extents) as the
operands.
In an expression that has intrinsic operators with a pointer as an
operand, the operation is performed on the value of the target
associated with the pointer.
For defined operators, operations on arrays and pointers are
determined by the procedure defining the operation.
A scalar is conformable with any array. If one operand of an
expression is an array and another operand is a scalar, it is as if
the value of the scalar were replicated to form an array of the
same shape as the array operand. The result is an array of the
same shape as the array operand.
The ranking assigned to each numeric intrinsic data type follows:
Data Type Ranking
--------- -------
LOGICAL*1 and BYTE lowest
LOGICAL*2 .
LOGICAL*4 .
LOGICAL*8 .
INTEGER*1 .
INTEGER*2 .
INTEGER*4 .
INTEGER*8 .
REAL (REAL*4) .
REAL*16 .
DOUBLE PRECISION (REAL*8) .
COMPLEX (COMPLEX*8) .
DOUBLE COMPLEX (COMPLEX*16) .
COMPLEX*32 highest
1 – Numeric
Numeric (arithmetic) expressions are formed with numeric operands
and numeric operators, and yield a single numeric value.
The term numeric includes logical data, because logical data is
treated as integer data when used in a numeric context. (.TRUE.
is -1; .FALSE. is 0.)
The numeric operators are as follows:
Operator Description
-----------------------
** exponentiation (evaluated
right to left)
* multiplication
/ division
+ addition
- subtraction
You can use parentheses to force an order of evaluation.
2 – Character
Character expressions consist of character items and character
operators. Evaluation of a character expression yields a single
value of character data type.
A character expression has the form:
character operand[//character operand]...
The concatenation operator (//) is the only character operator.
Concatenation is from left to right.
3 – Defined Operations
A defined operation is unary or binary. It is defined by a
function subprogram containing a generic interface block with the
specifier OPERATOR. A defined operation is not an intrinsic
operation. However, you can use a defined operation to extend the
meaning of an intrinsic operator.
For defined unary operations, the function must contain one
argument. For defined binary operations, the function must contain
two arguments.
Interpretation of the operation is provided by the function that
defines the operation.
A Fortran 95/90 defined operator can contain up to 31 letters, and
is enclosed in periods (.). Its name cannot be the same name as
any of the following:
o The intrinsic operators .NOT., .AND., .OR., .XOR.,
.EQV., .NEQV., .EQ., .NE., .GT., .GE., .LT., and .LE.
o The logical literal constants .TRUE. or .FALSE..
No two intrinsic operators can follow one another, but an intrinsic
or binary operator can be followed by a defined unary operator.
The result of a defined operation can have any type. The type of
the result (and its value) must be specified by the defining
function.
The following examples show expressions containing defined
operators:
.COMPLEMENT. A
X .PLUS. Y .PLUS. Z
M * .MINUS. N
4 – Initialization
An initialization expression must evaluate at compile time to a
constant. It is used to specify an initial value for an entity.
In an initialization expression, each operation is intrinsic and
each operand is one of the following:
o A constant or subobject of a constant
o An array constructor where each element, and the bounds and
strides of each implied-do are expressions whose primaries are
initialization expressions
o A structure constructor whose components are initialization
expressions
o An elemental intrinsic function reference of type integer or
character, whose arguments are initialization expressions of
type integer or character
o A reference to one of the following inquiry functions:
BIT_SIZE MINEXPONENT
DIGITS PRECISION
EPSILON RADIX
HUGE RANGE
ILEN SHAPE
KIND SIZE
LBOUND TINY
LEN UBOUND
MAXEXPONENT
Each function argument must be one of the following:
- An initialization expression
- A variable whose kind type parameter and bounds
are not assumed or defined by an ALLOCATE statement,
pointer assignment, or an expression that is not an
initialization expression
o A reference to one of the following transformational functions
(each argument must be an initialization expression):
REPEAT
RESHAPE
SELECTED_INT_KIND
SELECTED_REAL_KIND
TRANSFER
TRIM
o A reference to the transformational function NULL
o An implied-do variable within an array constructor where the
bounds and strides of the corresponding implied-do are
initialization expressions
o Another initialization expression enclosed in parentheses
Each subscript, section subscript, and substring starting and
ending point must be an initialization expression.
In an initialization expression, the exponential operator (**) must
have a power of type integer.
If an initialization expression invokes an inquiry function for a
type parameter or an array bound of an object, the type parameter
or array bound must be specified in a prior specification statement
(or to the left of the inquiry function in the same statement).
5 – Logical
Logical expressions can contain one or more logical operators and
logical, integer, or relational operands. The following are
logical operators:
Operator Meaning
---------------------------
.AND. Logical conjunction: the expression A .AND. B
is true if both A and B are true.
.OR. Logical disjunction (inclusive OR): the ex-
pression A .OR. B is true if either A, B, or
both, are true.
.XOR. Same as .NEQV.
.NEQV. Logical inequivalence (or exclusive OR): the
expression A .NEQV. B is true if either A or
B is true, but false if both are true.
.EQV. Logical equivalence: the expression A .EQV. B
is true if both A and B are true, or both are
false.
.NOT. Logical negation: the expression .NOT. A is
true if A is false and false if A is true.
6 – Operator Precedence
The following shows the precedence of all intrinsic and defined operators: Category Operator Precedence --------------------------------------------------- N/A Defined Unary Operators Highest Numeric ** . Numeric * or / . Numeric Unary + or - . Numeric Binary + or - . Character // . Relational .EQ.,.NE.,.LT.,.LE.,.GT.,.GE. . Logical .NOT. . Logical .AND. . Logical .OR. . Logical .XOR., .EQV., .NEQV. . N/A Defined Binary Operators Lowest
7 – Relational
Relational expressions consist of two or more expressions whose
values are compared to determine whether the relationship stated by
the relational operator is satisfied. The expression is reduced to
a logical value (true or false).
The following are relational operators:
Operator Meaning
------------------------------------------
.LT. or < Less than
.LE. or <= Less than or equal to
.EQ. or == Equal to
.NE. or /= Not equal to
.GT. or > Greater than
.GE. or >= Greater than or equal to
NOTE: Expressions of COMPLEX data type can use only
.EQ. and .NE. operators.
8 – Specification
A specification expression is a restricted expression that is of
type integer and has a scalar value. This type of expression
appears only in the declaration of array bounds and character
lengths.
In a restricted expression, each operation is intrinsic and each
operand is one of the following:
o A constant or subobject of a constant
o A variable that is one of the following:
- A dummy argument that does not have the OPTIONAL or
INTENT (OUT) attribute (or the subobject of such
a variable)
- In a common block (or the subobject of such a variable)
- Made accessible by use or host association (or the
subobject of such a variable)
o A structure constructor whose components are restricted
expressions
o An implied-do variable within an array constructor where the
bounds and strides of the corresponding implied-do are
initialization expressions
o A reference to one of the following inquiry functions:
BIT_SIZE NWORKERS
DIGITS PRECISION
EPSILON PROCESSORS_SHAPE
HUGE RADIX
ILEN RANGE
KIND SHAPE
LBOUND SIZE
LEN SIZEOF
MAXEXPONENT TINY
MINEXPONENT UBOUND
NUMBER_OF_PROCESSORS
Each function argument must be one of the following:
- A restricted expression
- A variable whose properties inquired about are not
dependent on the upper bound of the last dimension
of an assumed-size array, are not defined by an
expression that is a restricted expression, or are
not definable by an ALLOCATE or pointer assignment
statement.
o A reference to any other intrinsic function where each argument
is a restricted expression.
o A reference to a specification function (see below) where each
argument is a restricted expression
o An array constructor where each element, and bounds and strides
of each implied-do are expressions whose primaries are
restricted expressions
o Another restricted expression enclosed in parentheses
Each subscript, section subscript, and substring starting and
ending point must be a restricted expression.
Specification functions can be used in specification expressions to
indicate the attributes of data objects. A specification function
is a pure function. It cannot have a dummy procedure argument or
be any of the following:
o An intrinsic function
o An internal function
o A statement function
o Defined as RECURSIVE
A variable in a specification expression must have its type and
type parameters (if any) specified in one of the following ways:
o By a previous declaration in the same scoping unit
o By the implicit typing rules currently in effect for the
scoping unit
o By host or use association
If a variable in a specification expression is typed by the
implicit typing rules, its appearance in any subsequent type
declaration statement must confirm the implied type and type
parameters.
If a specification expression invokes an inquiry function for a
type parameter or an array bound of an object, the type parameter
or array bound must be specified in a prior specification statement
(or to the left of the inquiry function in the same statement).