Previous Table of Contents Next


1.2.11. Expressions

An expression can be used to indicate many sorts of computations and manipulations of data values.

1.2.11.1. Primaries

The basic component of an expression is a primary. Primaries are combined with operations and grouped with parentheses to indicate how values are to be computed. A primary is a constant, variable, function reference, array element, array section, structure component, substring, array constructor, structure constructor, or an expression enclosed in parentheses.

1.2.11.2. The Interpretation of Expressions

When more than one operation occurs in an expression, parentheses and the precedence of the operations determine the operands to which the operations are applied. Operations with the highest precedence are applied first to the operand or operands immediately adjacent to the operator. For example, because * has higher precedence than +, in the expression a + b * c, the multiplication is first applied to its operands b and c; then the result of this computation is used as an operand by adding it to the value of a.

When two operators have the same precedence, they are applied left to right, except for exponentiation, which is applied right to left. Thus, the value of 9 - 4 - 3 is 5 - 3 = 2, but the value of 2 ** 3 ** 2 is 29= 512.

Table 1.3 shows the operations with the highest precedence at the top of the list and the ones with the lowest precedence at the bottom.

Table 1.3. Operator precedence.

Operator Precedence

User-defined unary operation Highest
** .
* or / .
unary + or .
binary + or .
// .
==, /=, <, <=, >, >= .
.not. .
.and. .
.or. .
.eqv. or .neqv. .
User-defined binary operation Lowest

1.2.11.3. The Evaluation of Expressions

After it has been determined by the use of parentheses and precedence of operations which operations are to be performed on which operands, the computer may evaluate the expression by doing the computations in any order that is mathematically equivalent to the one indicated by the correct interpretation—except that it must evaluate each subexpression within parentheses before combining it with any other value. For example, the interpretation of the expression a + b + c indicates that a and b are to be added and the result added to c. Once this interpretation is made, it can be determined that a mathematically equivalent result will be obtained by first adding b and c and then adding this sum to a. Thus, the computer may do the computation either way. However, if the programmer writes the expression (a + b) + c, the computer must first do the computation as required by the parentheses.

1.2.12. Assignment

The assignment statement is the most common way of giving a variable a value. An assignment statement consists of a variable, an equals sign (=), and an expression. The expression is evaluated and assigned to the variable.

An example of an assignment statement is

   x = a + 2 * sin (b)

The variable on the left-hand side may be an array, an array element, an array section, a substring, or a structure component.

Complete agreement of the variable and expression type and kind is not always required. In some cases, the data type or kind parameter of the expression may be converted to assign it to the variable. If the variable on the left-hand side is any numeric type, the expression may be any numeric type and any kind. If the variable is type character, the expression must be type character, and they must be the same kind. If the variable is type logical, the expression must be type logical but may be any kind. If the variable is a derived type, the expression must be the same derived type. All of these rules apply to assignment as provided by the system (intrinsic assignment); it is possible to extend the meaning of assignment to other cases.


Previous Table of Contents Next