Expressions |
3 |
![]() |
There are three kinds of expressions:
The operands indicate what items to apply the action to. An operand can be any of the following kinds of data items:
indicates a nonstandard feature.
|
Operator |
Meaning |
|
** * / - + |
Exponentiation Multiplication Division Subtraction or Unary Minus Addition or Unary Plus |
If BYTE or LOGICAL operands are combined with arithmetic operators, they are interpreted as integer data.
a |
where a and b are operands, and
is any one of the **, *, /, -, or + operators.
A-Z X*B |
The operators + and - are unary operators in an expression of the form:
|
where b is an operand, and
is either of the - or + operators.
-Z +B |
Basic Arithmetic Expressions
Each arithmetic operator is shown in its basic expression in the following table:
|
Expression |
Meaning |
|
a ** z a / z a * z a - z -z a + z +z |
Raise a to the power z Divide a by z Multiply a by z Subtract z from a Negate z Add z to a Same as z |
|
Operator |
Precedence |
|
** * / + -
|
First Second Last
|
For the left-to-right rule, the one exception is shown by the following example:
F ** S ** Z |
F ** (S ** Z) |
f77 allows two successive operators.
X ** -A * Z |
The above expression is evaluated as follows:
X ** (-(A * Z)) |
In the above example, the compiler starts to evaluate the **, but it needs to know what power to raise X to; so it looks at the rest of the expression and must choose between - and *. It first does the *, then the -, then the **.
Mixed Mode
If both operands have the same type, then the resulting value has that type. If operands have different types, then the weaker of two types is promoted to the stronger type, where the weaker type is the one with less precision or fewer storage units. The ranking is summarized in the following table:
Example of mixed mode: If R is real, and I is integer, then the expression:
Note - REAL*4, INTEGER*8, and LOGICAL*8 are of the same rank, but they can be the results of different pairs of operands. For example, INTEGER*8 results if you combine INTEGER*8 and any of the types between 1-5. Likewise, REAL*4 results if one of the operands is REAL*4, and the other is any of the types between 1-5. LOGICAL*8 dictates only the 8-byte size of the result.
R * I |
has the type real, because first I is promoted to real, and then the multiplication is performed.
Rules
Note these rules for the data type of an expression:
2/3 + 3/4 |
You can use a logical value any place where the FORTRAN Standard requires a numeric value. The numeric can be integer, real, complex, double precision, double complex, or real*16 (SPARC and PowerPC only). The compiler implicitly converts the logical to the appropriate numeric. Logical operations are allowed on integers, bytes, and characters. If you use these features, your program may not be portable.
If the operands are mixed integer and logical, then the logicals are converted to integers, and the result is an integer.
|
v = e | |
|
e |
Arithmetic expression, a character constant, or a logical expression |
|
v |
Numeric variable, array element, or record field |
Assigning logicals to numerics is allowed, but nonstandard, and may not be portable. The resultant data type is, of course, the data type of v.
Example: Arithmetic assignment:
Note - Compiling with any of the options -i2, -dbl, -r8, or -xtypemap will have an effect on the assumed type of e. This is discussed in Chapter 2. See also the Sun Fortran User's Guide for a description of these options.
Character Expressions
A character expression is an expression whose operands have the character type. It evaluates to a single value of type character, with a size of one or more characters. The only character operator is the concatenation operator, //.
|
Expression |
Meaning |
|
a // z |
Concatenate a with z. |
The result of concatenating two strings is a third string that contains the characters of the left operand followed immediately by the characters of the right operand. The value of a concatenation operation a//z is a character string whose value is the value of a concatenated on the right with the value of z, and whose length is the sum of the lengths of a and z.
'wxy' 'AB' // 'wxy' C C // S C(4:7) R.C |
Note the following exceptions:
--One way to enter control characters is to hold down the Control key and press another key. Most control characters can be entered this way, but not Control-A, Control-B, Control-C, or Control-J.
CHARACTER etx etx = CHAR(3) |
--Multiple byte characters, such as Kanji, are allowed in comments and strings.
|
v = e | |
|
e |
Expression giving the value to be assigned |
|
v |
Variable, array element, substring, or character record field |
The meaning of character assignment is to copy characters from the right to the left side.

:
CHARACTER A*4, B*2, C*8 A = 'join' B = 'ed' C = A // B PRINT *, C END |
Also, this program displays the equal string:
IF ( ('ab' // 'cd') .EQ. 'abcd' ) PRINT *, 'equal'
END |
Example: Character assignment:
CHARACTER BELL*1, C2*2, C3*3, C5*5, C6*6 REAL Z C2 = 'z' C3 = 'uvwxyz' C5 = 'vwxyz' C5(1:2) = 'AB' C6 = C5 // C2 I = 'abcd' Z = 'wxyz' BELL = CHAR(7) ! Control Character (^G) |
|
C2 |
gets |
'z |
A trailing blank |
|
C3 |
gets |
'uvw' |
|
|
C5 |
gets |
'ABxyz' |
|
|
C6 |
gets |
'ABxyzz' |
That is, the 'z' from C2 |
|
I |
gets |
'abcd' |
|
|
Z |
gets |
'wxyz' |
|
|
BELL |
gets |
07 hex |
Control-G, a bell |
Example 4: A Hollerith assignment:
CHARACTER S*4 INTEGER I2*2, I4*4 REAL R S = 4Hwxyz I2 = 2Hyz I4 = 4Hwxyz R = 4Hwxyz |
Rules of Assignment
Here are the rules for character assignments:
CHARACTER S*8 S = 'abcdefgh' S(4:6) = S(5:7) WRITE(*,*) S END |
Logical Expressions
A logical expression is a sequence of one or more logical operands and logical operators. It evaluates to a single logical value. The operators can be any of the following.
The period delimiters are necessary.
|
Operator |
Precedence |
|
.NOT. .AND. .OR. .NEQV.,.XOR., .EQV. |
Highest
Lowest |
If the logical operators are of equal precedence, they are evaluated left to right.
|
Operator |
Precedence |
|
Arithmetic Character Relational Logical |
Highest
Lowest |
The following table shows the meanings of simple expressions:
This is the syntax for the assignment of the value of a logical expression to a logical variable:
|
v = e | |
|
e |
A logical expression, an integer between -128 and 127, or a single character constant |
|
v |
A logical variable, array element, or record field |
Execution of a logical assignment statement causes evaluation of the logical expression e and assignment of the resulting value to v. If e is a logical expression, rather than an integer between -128 and 127, or a single character constant, then e must have a value of either true or false.
LOGICAL B1*1, B2*1 LOGICAL L3, L4 B2 = B1 B1 = L3 L4 = .TRUE. |
Relational Operator
A relational operator compares two arithmetic expressions, or two character expressions, and evaluates to a single logical value. The operators can be any of the following:
|
Operator |
Meaning |
|
.LT. .LE. .EQ. .NE. .GT. .GE. |
Less than Less than or equal Equal Not equal Greater than Greater than or equal |
The period delimiters are necessary.
NODE .GE. 0 X .LT. Y U*V .GT. U-V M+N .GT. U-V Mixed mode: integer M+N is promoted to real STR1 .LT. STR2 where STR1 and STR2 are type character S .EQ. 'a' where S is type character |
For character relational expressions:
Examples: Constant expressions:
There are a few restrictions on constant expressions:
demo% cat ConstExpr.f parameter (T=2.0*(3.0**2.5)) write(*,*) t end demo% f77 ConstExpr.f ConstExpr.f: MAIN: "ConstExpr.f", line 1: Warning: parameter t set to a nonconstant demo% a.out 31.1769 demo% |
|
v = e | |
|
e |
A record or record field |
|
v |
A record or record field |
Both e and v must have the same structure. That is, each must have the same number of fields, and corresponding fields must be of the same type and size.
In the above example, the first assignment statement copies one whole record (all five fields) to another record; the second assignment statement copies a whole record into the first element of an array of records; the WRITE statement writes a whole record; and the last statement sets the ID of one record to 82.
Evaluation of Expressions
The following restrictions apply to all arithmetic, character, relational, and logical expressions: