## Chapter 3. Expressions

An expression performs a specified type of computation. It is composed of a sequence of operands, operators, and parentheses. The following are the types of Fortran expressions:

This chapter describes formation, interpretation, and evaluation rules for each type of expression. This chapter also discusses mixed-mode expressions.

## Arithmetic Expressions

An arithmetic expression specifies a numeric computation that yields a numeric value on evaluation. The simplest form of an arithmetic expression can be

• an unsigned arithmetic constant

• a symbolic name of an arithmetic constant

• an arithmetic variable reference

• an arithmetic array element reference

• an arithmetic function reference

You can form more complicated arithmetic expressions from one or more operands together with arithmetic operators and parentheses.

An arithmetic element can include logical entities because logical data is treated as integer data when used in an arithmetic context. When both arithmetic and logical operands exist for a given operator, the logical operand is promoted to type INTEGER of the same byte length as the original logical length. For example, a LOGICAL*2 will be promoted to INTEGER*2 and a LOGICAL*4 will be promoted to INTEGER*4.

### Arithmetic Operators

Table 3-1 shows the arithmetic operators.

Table 3-1. Arithmetic Operators

Operator

Function

**

Exponentiation

*

Multiplication

/

Division

+

-

Subtraction or negation

Use the exponentiation, division, and multiplication operators between exactly two operands. You can use the addition and subtraction operators with one or two operands; in the latter case, specify the operator before the operand; for example, -TOTAL.

Do not specify two operators in succession unless it is a unary operator following another operator. Note that the exponentiation operator consists of the two characters (**), but is a single operator. Implied operators, as in implied multiplication, are not allowed.

### Interpretation of Arithmetic Expressions

Table 3-2 interprets sample arithmetic expressions.

Table 3-2. Interpretation of Arithmetic Expressions

Operator

Use

Interpretation

**

x1 ** x2

Exponentiate x1 to the power of x2

*

x1 * x2

Multiply x1 and x2

/

x1 / x2

Divide x1 by x2

+

x1 + x2

+ x

x (identity)

-

x1 - x2

Subtract x1 from x2

-x

Negate x

An arithmetic expression containing two or more operators is interpreted based on a precedence relation among the arithmetic operators. This precedence, from highest to lowest, is

• ( )

• **

• * and /

• + and -

Use parentheses to override the order of precedence.

The following is an example of an arithmetic expression:

 `A/B-C**D `

The operators are executed in the following sequence:

1. C**D is evaluated first.

2. A/B is evaluated next.

3. The result of C**D is subtracted from the result of A/B to give the final result.

A unary operator (-) can follow another operator. Specifying the unary operator after the exponentiation operator produces a variation on the standard order of operations. The unary operator is evaluated first in that case, resulting in exponentiation taking a lower precedence in the expression.

For example, the following expression

 `A ** - B * C`

is interpreted as

 `A ** ( - B * C ) `

### Arithmetic Operands

Arithmetic operands must specify values with integer, real, double-precision, complex, or double-complex data types. You can combine specific operands in an arithmetic expression. The arithmetic operands, in order of increasing complexity, are

• primary

• factor

• term

• arithmetic expression

A primary is the basic component in an arithmetic expression. The forms of a primary are the following:

• an unsigned arithmetic constant

• a symbolic name of an arithmetic constant

• an arithmetic variable reference

• an arithmetic array element reference

• an arithmetic function reference

• an arithmetic expression enclosed in parentheses

A factor consists of one or more primaries separated by the exponentiation operator. The following are the forms of a factor:

• primary

• primary ** factor

Factors with more than one exponentiation operator are interpreted from right to left. For example, I**J**K is interpreted as I**(J**K), and I**J**K**L is interpreted as I**(J**(K**L)).

The term incorporates multiplicative operators into arithmetic expressions. Its forms are the following:

• factor

• term/factor

• term * factor

The above definition indicates that factors are combined from left to right in a term containing two or more multiplication or division operators.

Finally, at the highest level of the hierarchy, are the arithmetic expressions. The forms of an arithmetic expression are

• term

• + term

• - term

• arithmetic expression + term

• arithmetic expression - term

An arithmetic expression consists of one or more terms separated by an addition operator or a subtraction operator. The terms are combined from left to right. For example, A+B-C has the same interpretation as the expression (A+B)-C.

An arithmetic expression can begin with a plus or minus sign.

### Arithmetic Constant Expressions

An arithmetic constant expression is an arithmetic expression containing no variables. Thus, each primary in an arithmetic constant expression must be one of the following:

• arithmetic constant

• symbolic name of an arithmetic constant

• arithmetic constant expression enclosed in parentheses

In an arithmetic constant expression, do not specify the exponentiation operator unless the exponent is of type integer. Variable, array element, and function references are not allowed. Examples of integer constant expressions are

 ```7 -7 -7+5 3**2 x+3```

where x is the symbolic name of a constant

### Integer Constant Expressions

An integer constant expression is an arithmetic constant expression containing only integers. It can contain constants or symbolic names of constants, provided they are of type integer. As with all constant expressions, no variables, array elements, or function references are allowed.

### Evaluating Arithmetic Expressions

The data type of an expression is determined by the data types of the operands and functions that are referenced. Thus, integer expressions, real expressions, double-precision expressions, complex expressions, and double-complex expressions have values of type integer, real, double-precision, complex, and double-complex, respectively.

#### Single-Mode Expressions

Single-mode expressions are arithmetic expressions in which all operands have the same data type. The data type of the value of a single-mode expression is thus the same as the data type of the operands. When the addition operator or the subtraction operator is used with a single operand, the data type of the resulting expression is the same as the data type of the operand.

#### Mixed-Mode Expressions

Mixed-mode expressions contain operands with two or more data types. If every element in an arithmetic expression is of the same type, the value produced by the expression is also of that same type. If elements of different data types are combined in an expression, the evaluation of that expression and the data type of the resulting value depend on the ranking associated with each data type.

Data type rankings shows the ranking assigned to each data type, where the lowest ranking is 1.

Data type rankings

 Data Type Rank BYTE 1 LOGICAL*1 1 LOGICAL*2 2 LOGICAL*4 3 LOGICAL*8 4 INTEGER*1 5 INTEGER*2 6 INTEGER*4 7 INTEGER*8 8 REAL*4 9 REAL*8 10 REAL*16 11 COMPLEX*8 12 COMPLEX*16 13 COMPLEX*32 14

The data type of the value produced by an operation on two arithmetic elements of different data types is the data type of the highest-ranking element of the operation. For example, the data type of the value resulting from an operation on an INTEGER and a REAL element is REAL. However, an operation involving a COMPLEX*8 data type and a REAL*8 data type produces a COMPLEX*16 data result. Similarly, an operation involving either a COMPLEX*8 data type or a COMPLEX*16 data type and a REAL*16 data type produces a COMPLEX*32 data result.

LOGICAL items in an INTEGER context are treated as type INTEGER. Thus, a LOGICAL*8 combined with an INTEGER*1 produce the same result type as an INTEGER*8 combined with an INTEGER*1 or an INTEGER*8.

### Exponentiation

Exponentiation is an exception to the above rules for mixed-mode expressions. When raising a value to an integer power, the integer is not converted. The result is assigned the type of the left operand.

When a complex value is raised to a complex power, the value of the expression is defined as follows:

 `xy = EXP (y * LOG(x))`

### Integer Division

One operand of type INTEGER can be divided by another operand of type INTEGER. The result of an integer division operation is a value of type integer, referred to as an integer quotient. The integer quotient is obtained as follows:

• If the magnitude of the mathematical quotient is less than one, then the integer quotient is zero. For example, the value of the expression (18/30) is zero.

• If the magnitude of the mathematical quotient is greater than or equal to one, then the integer quotient is the largest integer that does not exceed the magnitude of the mathematical quotient and whose sign is the same as that of the mathematical quotient. For example, the value of the expression (-9/2) is -4.

## Character Expressions

A character expression yields a character string value on evaluation. Character expressions are built up from the following simple elements:

• character constant or symbolic name of a character constant

• character function reference

• character variable reference

• character array element reference

• character substring reference

Construct complicated character expressions from one or more of these elements using the concatenate operator and parentheses.

### Character Constant Expressions

A character constant expression is made up of operands that cannot vary. Each primary in a character constant expression must be one of the following:

• a character constant

• a symbolic name of a character constant

• a character constant expression enclosed in parentheses

For the details of character constant syntax see “Character Constants” in Chapter 2 and “Hollerith Constants” in Chapter 2. A character constant expression cannot contain variable, array element, substring, or function references.

### Character Substrings

A character substring is a contiguous sequence of characters that is part of a character data item. A character substring cannot be empty; that is, it must contain at least one byte of data.

#### Substring Names

A substring name defines the corresponding substring and allows it to be referenced in a character expression. A substring name has one of the following forms:

 `var([e1]:[e2])`
 `arra(sub[,sub]...) ([e1]:[e2])`

The following arguments are used:

 var a character variable name. arra a character array name. sub a subscript expression. e1 and e2 integer expressions. You can specify a non-integer expression for e1 and e2, but a non-integer value is truncated to an integer before use.

The value e1 specifies the leftmost character of the substring relative to the beginning of the variable or array element, while e2 specifies the rightmost character. Characters are numbered left to right beginning with 1. For example, the following denotes the third through the fifth characters in the character variable EX:

 `EX(3:5)`

The following specifies the first through the fifth characters the character array element NAME(2,4):

 `NAME(2,4)(1:5)`

A character substring has the length e2- e1+1.

#### Substring Values e1, e2

The value of the expressions e1 and e2 in a substring name must fall (after truncation to integer) within the range 1 ≤ e1 e2len where len is the length of the character variable or array element.

When e1 is omitted, a value of 1 is assumed. When e2 is omitted, a value oflen is assumed. When both e1 and e2 are omitted, the result is to take all of the characters. Thus var(:) is equivalent to var and arra( s [,s]...)(:) is equivalent to arra(s [, s]...).

The expressions e1 and e2 can be any integer expression, including array element references and function references. Consider the following character variable:

 `XCHAR = 'QRSTUVWXYZ' `

Table 3-3 lists examples of valid substrings taken from this variable.

Table 3-3. Valid Substring Examples

Expression

Substring Value

Substring Length

EX1 = XCHAR (3:8)

STUVWX

6

EX2 = XCHAR (:8)

QRSTUVWX

8

EX3 = XCHAR (5:)

UVWXYZ

6

EX4 = XCHAR(:)

QRSTUVWXYZ

10

EX5 = XCHAR(IA:IA+7)

depends on IA

8

EX6 = XCHAR(INDEX(XCHAR,'VW'):)

VWXYZ

5

### Concatenate Operator

The concatenate operator (//) is the only character operator defined in Fortran. A character expression formed from the concatenation of two character operands x1 and x2 is specified as the following:

 `x1 // x2 `

The result of this operation is a character string with a value of x1 extended on the right with the value of x2. The length of the result is the sum of the lengths of the character operands. For example,

 `'HEL' // 'LO2' `

The result of the above expression is the string HELLO2 of length six.

Except in a character assignment statement, concatenation of an operand with an asterisk (*) as its length specification is not allowed unless the operand is the symbolic name of a constant.

#### Parenthesized Character Expressions

In a character expression build from two or more concatenation operators, the elements are combined from left to right. Thus, the character expression

 `'A' // 'BCD' // 'EF' `

is interpreted the same as

 `('A' // 'BCD') // 'EF' `

The value of the above character expression is ABCDEF.

## Relational Expressions

A relational expression yields a logical value of either .TRUE. or .FALSE. on evaluation and comparison of two arithmetic expressions or two character expressions. A relational expression can appear only within a logical expression. Refer to “Logical Expressions” for details about logical expressions.

### Relational Operators

Table 3-4 lists the Fortran relational operators. Arithmetic and character operators are evaluated before relational operators.

Table 3-4. Fortran Relational Operators

Relational Operator

Meaning

.EQ.

Equal to

.NE.

Not equal to

.GT.

Greater than

.GE.

Greater than or equal to

.LT.

Less than

.LE.

Less than or equal to

### Relational Operands

The operands of a relational operator can be arithmetic or character expressions. The relational expression requires exactly two operands and is written in the following form:

e1 relop e2

where e1 and e2 are arithmetic or character expressions and relop is the relational operator. Note: Both e1 and e2 must be the same type of expression, either arithmetic or character.

### Evaluating Relational Expressions

The result of a relational expression is of type logical, with a value of .TRUE. or .FALSE.. The manner in which the expression is evaluated depends on the data type of the operands.

### Arithmetic Relational Expressions

In an arithmetic relational expression, e1 and e2 must each be an integer, real, double precision, complex, or double complex expression. relop must be a relational operator.

The following are examples of arithmetic relational expressions:

 ```(a + b) .EQ. (c + 1) HOURS .LE. 40```

You can use complex type operands only when specifying either the .EQ. or .NE. relational operator.

An arithmetic relational expression has the logical value .TRUE. only if the values of the operands satisfy the relation specified by the operator. Otherwise, the value is .FALSE..

If the two arithmetic expressionse1 and e2 differ in type, the expression is evaluated as follows:

 `((e1) - (e2)) relop 0`

where the value 0 (zero) is of the same type as the expression ((e1)- (e2)) and the type conversion rules apply to the expression. Do not compare a double precision value with a complex value.

### Character Relational Expressions

In a character relational expression, e1 and e2 are character expressions and relop is a relational operator.

The following is an example of a character relational expression:

 `NAME .EQ. 'HOMER'`

A character relational expression has the logical value .TRUE. only if the values of the operands satisfy the relation specified by the operator. Otherwise, the value is .FALSE.. The result of a character relational expression depends on the collating sequence as follows:

• If e1 and e2 are single characters, their relationship in the collating sequence determines the value of the operator. e1 is less than or greater than e2 if e1 is before or after e2, respectively, in the collating sequence.

• If either e1 or e2 are character strings with lengths greater than 1, corresponding individual characters are compared from left to right until a relationship other than .EQ. can be determined.

• If the operands are of unequal length, the shorter operand is extended on the right with blanks to the length of the longer operand for the comparison.

• If no other relationship can be determined after the strings are exhausted, the strings are equal.

The collating sequence depends partially on the processor; however, equality tests .EQ. and .NE. do not depend on the processor collating sequence and can be used on any processor.

## Logical Expressions

A logical expression specifies a logical computation that yields a logical value. The simplest form of a logical expression is one of the following:

• logical constant

• logical variable reference

• logical array element reference

• logical function reference

• relational expression

Construct complicated logical expressions from one or more logical operands together with logical operators and parentheses.

### Logical Operators

Table 3-5 defines the Fortran logical operators.

Table 3-5. Logical Operators

Logical Operator

Meaning

.NOT.

Logical negation

.AND.

Logical conjunt

.OR.

Logical disjunct

.EQV.

Logical equivalence

.NEQV.

Logical exclusive or

.XOR.

Same as .NEQV.

All logical operators require at least two operands, except the logical negation operator .NOT. , which requires only one.

A logical expression containing two or more logical operators is evaluated based on precedence of the logical operators. This precedence, from highest to lowest, is

• .NOT.

• .AND.

• .OR.

• .EQV. and .NEQV.

• .XOR.

For example, in the following expression:

 `W .NEQV. X .OR. Y .AND. Z `

the operators are executed in the following sequence:

1. Y .AND. Z is evaluated first (A represents the result).

2. X .OR. A is evaluated second (B represents the result).

3. W .NEQV. B is evaluated to produce the final result.

You can use parentheses to override the precedence of the operators.

### Logical Operands

Logical operands specify values with a logical data type. The forms of a logical operands are the following:

• logical primary

• logical factor

• logical term

• logical disjunct

• logical expression

#### Logical Primary

The logical primary is the basic component of a logical expression. The forms of a logical primary are the following:

• logical constant

• symbolic name of a logical constant

• integer or logical variable reference

• logical array element reference

• integer or logical function reference

• relational expression

• integer or logical expression in parentheses

When an integer appears as an operand to a logical operator, the other operand is promoted to type integer if necessary and the operation is performed on a bit-by-bit basis producing an integer result. Whenever an arithmetic datum appears in a logical expression, the result of that expression will be of type integer because of type promotion rules. If necessary, the result can be converted back to LOGICAL.

Do not specify two logical operators consecutively and do not use implied logical operators.

#### Logical Factor

The logical factor uses the logical negation operator .NOT. to reverse the logical value to which it is applied. For example, applying .NOT. to a false relational expression makes the expression true. Therefore, if UP is true, .NOT. UP is false. The logical factor has the following forms:

• logical primary

• .NOT. logical primary

#### Logical Term

The logical term uses the logical conjunct operator .AND. to combine logical factors. It takes the following forms:

• logical factor

• logical term .AND. logical factor

In evaluating a logical term with two or more .AND. operators, the logical factors are combined from left to right. For example, X .AND. Y .AND. Z has the same interpretation as (X .AND. Y) .AND. Z.

#### Logical Disjunct

The logical disjunct is a sequence of logical terms separated by the .OR. operator and has the following two forms:

• logical term

• logical disjunct .OR. logical term

In an expression containing two or more .OR. operators, the logical terms are combined from left to right in succession. For example, the compiler interprets the expression X .OR. Y .OR. Z the same as(X .OR. Y) .OR. Z.

#### Logical Expression

At the highest level of complexity is the logical expression. A logical expression is a sequence of logical disjuncts separated by the .EQV., .NEQV., or .XOR. operators. Its forms are

• logical disjunct

• logical expression .EQV. logical disjunct

• logical expression .NEQV. logical disjunct

• logical expression .XOR. logical disjunct

The logical disjuncts are combined from left to right when a logical expression contains two or more .EQV., .NEVQ. , or .XOR. operators.

A logical constant expression is a logical expression in which each primary is a logical constant, the symbolic name of a logical constant, a relational expression in which each primary is a constant, or a logical constant expression enclosed in parentheses. A logical constant expression can contain arithmetic and character constant expressions but not variables, array elements, or function references.

### Interpretation of Logical Expressions

In general, logical expressions containing two or more logical operators are executed according to the hierarchy of operators described previously, unless the order has been overridden by the use of parentheses. Table 3-6 defines the form and interpretation of the logical expressions.

Table 3-6. Logical Expressions

A

B

.NOT. B

A .AND. B

A .OR. B

A .EQV. B

A .XOR. B

A .NEQV. B

F

F

T

F

F

T

F

F

T

F

F

T

F

T

T

F

T

F

T

F

T

T

T

F

T

T

T

F

## Evaluating Expressions

Several rules are applied to the general evaluation of Fortran expressions. This section covers the priority of the different Fortran operators, the use of parentheses in specifying the order of evaluation, and the rules for combining operators with operands. Note: Any variable, array element, function, or character substring in an expression must be defined with a value of the correct type at the time it is referenced.

### Precedence of Operators

Certain Fortran operators have precedence over others when combined in an expression. The previous sections have listed the precedence among the arithmetic, logical, and expression operators. No precedence exists between the relational operators and the single character operator (//). On the highest level, the precedence among the types of expression operators, from highest to lowest, is

• arithmetic

• character

• relational

• logical

### Integrity of Parentheses and Interpretation Rules

Use parentheses to specify the order in which operators are evaluated within an expression. Expressions within parentheses are treated as an entity.

In an expression containing more than one operation, the processor first evaluates any expressions within parentheses. Subexpressions within parentheses are evaluated beginning with the innermost subexpression and proceeding sequentially to the outermost. The processor then scans the expression from left to right and performs the operations according to the operator precedence described previously.