|Fortran 77 Language Reference|
This chapter discusses Fortran expressions and how they are evaluated.
Expressions, Operators, and Operands
An expression is a combination of one or more operands, zero or more operators, and zero or more pairs of parentheses.
There are three kinds of expressions:
- An arithmetic expression evaluates to a single arithmetic value.
- A character expression evaluates to a single value of type character.
- A logical or relational expression evaluates to a single logical value.
The operators indicate what action or operation to perform.
The operands indicate what items to apply the action to. An operand can be any of the following kinds of data items:
- Array element
- Structured record field (if it evaluates to a scalar data item)
- An expression
An arithmetic expression evaluates to a single arithmetic value, and its operands have the following types. indicates a nonstandard feature.
LOGICALoperands are combined with arithmetic operators, they are interpreted as integer data.
where a and b are operands, and is any one of the
where b is an operand, and is either of the
-or + operators.
Basic Arithmetic Expressions
Each arithmetic operator is shown in its basic expression in the following table:
TABLE 3-2 Arithmetic Expressions
a ** z
a / z
a * z
a - z
a + z
ato the power
In the absence of parentheses, if there is more than one operator in an expression, then the operators are applied in the order of precedence. With one exception, if the operators are of equal precedence, they are applied left to right.
TABLE 3-3 Arithmetic Operator Precedence
f77allows two successive operators.
In the above example, the compiler starts to evaluate the
**, but it needs to know what power to raise
Xto; so it looks at the rest of the expression and must choose between
*. It first does the
*, then the
-, then the
Except for BYTE operands, if both operands have the same type, then the resulting value has that type. (The result of an arithmetic operation with both operands type BYTE is default integer.) 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:
BYTE or LOGICAL*1(SPARC only)
REAL*8 (DOUBLE PRECISION)
REAL*16 (QUAD PRECISION)
COMPLEX*8 (COMPLEX)(SPARC only)
COMPLEX*16 (DOUBLE COMPLEX)
COMPLEX*32 (QUAD COMPLEX)
LOGICAL*8are of the same rank, but they can be the results of different pairs of operands. For example,
INTEGER*8results if you combine
INTEGER*8and any of the types between 1-5. Likewise,
REAL*4results if one of the operands is
REAL*4, and the other is any of the types between 1-5.
LOGICAL*8dictates only the 8-byte size of the result.
has the type real, because first
Iis promoted to real, and then the multiplication is performed.
Note these rules for the data type of an expression:
- If there is more than one operator in an expression, then the type of the last operation performed becomes the type of the final value of the expression.
- Integer operators apply to only integer operands.
- There is one extension to this: a logical or byte operand in an arithmetic context is used as an integer.
- Real operators apply to only real operands, or to combinations of byte, logical, integer, and real operands. An integer operand mixed with a real operand is promoted to real; the fractional part of the new real number is zero. For example, if
Ris real, and
Iis integer, then
R+Iis real. However,
- Double precision operators apply to only double precision operands, and any operand of lower precision is promoted to double precision. The new least significant bits of the new double precision number are set to zero. Promoting a real operand does not increase the accuracy of the operand.
- Complex operators apply to only complex operands. Any integer operands are promoted to real, and they are then used as the real part of a complex operand, with the imaginary part set to zero.
- Numeric operations are allowed on logical variables. You can use a logical value any place where the FORTRAN Standard requires a numeric value. The numeric can be
real*16(SPARC only). The compiler implicitly converts the logical to the appropriate numeric. If you use these features, your program may not be portable.
- Example: Some combinations of both
COMPLEX C1 / ( 1.0, 2.0 ) /INTEGER*2 I1, I2, I3LOGICAL L1, L2, L3, L4, L5REAL R1 / 1.0 /DATA I1 / 8 /, I2 / 'W' /, I3 / 0 /DATA L1/.TRUE./, L2/.TRUE./, L3/.TRUE./, L4/.TRUE./,& L5/.TRUE./L1 = L1 + 1I2 = .NOT. I2L2 = I1 .AND. I3L3 = I1 .OR. I2L4 = L4 + C1L5 = L5 + R1
For integer operands with a logical operator, the operation is done bit by bit. The result is an integer.
If the operands are mixed integer and logical, then the logicals are converted to integers, and the result is an integer.
The arithmetic assignment statement assigns a value to a variable, array element, or record field. The syntax is:
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.
Execution of an arithmetic assignment statement causes the evaluation of the expression e, and conversion to the type of v (if types differ), and assignment of v with the resulting value typed according to the table below.
Character constants can be assigned to variables of type integer or real. Such a constant can be a Hollerith constant or a string in apostrophes or quotes. The characters are transferred to the variables without any conversion of data. This practice is nonstandard and may not be portable.
DOUBLE PRECISION(SPARC only)
Note Compiling with any of the options
-xtypemapwill have an effect on the assumed type of e. This is discussed in Chapter 2. See also the Fortran User's Guide for a description of these options.
Example: Arithmetic assignment:
INTEGER I2*2, J2*2, I4*4LOGICAL L1, L2REAL R4*4, R16*16DOUBLE PRECISION DPCOMPLEX C8, C16*16J2 = 29002I2 = J2I4 = (I2 * 2) + 1DP = 6.4D0QP = 9.8Q1R4 = DPR16 = QPC8 = R1C8 = ( 3.0, 5.0 )I2 = C8C16 = C8C8 = L1R4 = L2
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,
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//zis a character string whose value is the value of
aconcatenated on the right with the value of
z, and whose length is the sum of the lengths of
The operands can be any of the following kinds of data items:
- Character constant
- Character variable
- Character array element
- Character function
- Structured record field (if it evaluates to a scalar character data item)
Note the following (nonstandard) exceptions:n Control characters--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 String Assignment
The meaning of character assignment is to copy characters from the right to the left side.
Execution of a character assignment statement causes evaluation of the character expression and assignment of the resulting value to v.
- If e is longer than v, characters on the right are truncated.
- If e is shorter than v, blank characters are padded on the right.
Example: Character assignment:
CHARACTER BELL*1, C2*2, C3*3, C5*5, C6*6REAL ZC2 = 'z'C3 = 'uvwxyz'C5 = 'vwxyz'C5(1:2) = 'AB'C6 = C5 // C2I = 'abcd'Z = 'wxyz'BELL = CHAR(7) Control Character (^G)
Rules of Assignment
Here are the rules for character assignments:
- If the left side is longer than the right, it is padded with trailing blanks.
- If the left side is shorter than the right, trailing characters are discarded.
- The left and right sides of a character substring assignment cannot overlap. See the Substrings .
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.
TABLE 3-4 Logical Operators
Logical disjunction (inclusive
The period delimiters are necessary.
Two logical operators cannot appear consecutively, unless the second one is the
If the logical operators are of equal precedence, they are evaluated left to right.
If the logical operators appear along with the various other operators in a logical expression, the precedence is as follows.
TABLE 3-6 Operator Precedence Arithmetic
The following table shows the meanings of simple expressions:
TABLE 3-7 Logical Expressions and Their Meanings
X .AND. Y
X .OR. Y
X .NEQV. Y
X .XOR. Y
X .EQV. Y
Y, or both, are true.
Yare not both true and not both false.
Yis true, but not both.
Yare both true or both false.
Logical operations on operands of different sizes result in a logical value promoted to the size of the largest operand. For example
BYTEvariables are treated the same as
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 expressions of any size can be assigned to logical variables of any size.
Assigning numerics to logicals is allowed. (All non-zero values are treated as
.TRUE., and zero is
.FALSE.) This practice is nonstandard, however, and is not portable.
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:
TABLE 3-8 Relational Operators
Less than or equal
Greater than or equal
The period delimiters are necessary.
All relational operators have equal precedence. Character and arithmetic operators have higher precedence than relational operators.
For a relational expression, first each of the two operands is evaluated, and then the two values are compared. If the specified relationship holds, then the value is true; otherwise, it is false.
Example: Relational operators:
NODE .GE. 0X .LT. YU*V .GT. U-VM+N .GT. U-V Mixed mode: integer M+N is promoted to realSTR1 .LT. STR2 STR1 and STR2 are character typeS .EQ. 'a' S is character type
For character relational expressions:
- "Less than" means "precedes in the ASCII collating sequence."
- If one operand is shorter than the other, the shorter one is padded on the right with blanks to the length of the longer.
A constant expression is made up of explicit constants and parameters and the FORTRAN operators. Each operand is either itself another constant expression, a constant, a symbolic name of a constant, or one of the intrinsic functions called with constant arguments.
Examples: Constant expressions:
PARAMETER (L=29002), (P=3.14159), (C='along the ')PARAMETER ( I=L*2, V=4.0*P/3.0, S=C//'riverrun' )PARAMETER ( M=MIN(I,L), IA=ICHAR('A') )PARAMETER ( Q=6.4Q6, D=2.3D9 )K = 66 * 80VOLUME = V*10**3DO I = 1, 20*3
There are a few restrictions on constant expressions:
- Constant expressions are permitted wherever a constant is allowed, except they are not allowed in
- Constant expressions are permitted in variable format expressions.
- Exponentiation to a floating-point power is not allowed; a warning is issued.
- Example: Exponentiation to a floating-point power is not allowed:
cat ConstExpr.fparameter (T=2.0*(3.0**2.5))write(*,*) tenddemo%
f77 ConstExpr.fConstExpr.f:MAIN:"ConstExpr.f", line 1: Warning:parameter t set to a nonconstantdemo%
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.
Example: A record assignment and a record-field assignment:
STRUCTURE /PRODUCT/INTEGER*4 IDCHARACTER*16 NAMECHARACTER*8 MODELREAL*4 COSTREAL*4 PRICEEND STRUCTURERECORD /PRODUCT/ CURRENT, PRIOR, NEXT, LINE(10)...CURRENT = NEXTLINE(1) = CURRENTWRITE ( 9 ) CURRENTNEXT.ID = 82
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
WRITEstatement writes a whole record; and the last statement sets the
IDof one record to 82.
Evaluation of Expressions
The following restrictions apply to all arithmetic, character, relational, and logical expressions:
- If you reference any one of these items in an expression, variable, array element, character substring, record field, pointer, or function, then that item must be defined at the time the reference is executed.
- An integer operand must be defined with an integer value, and not with a statement label value by an
- All the characters of a substring that are referenced must be defined at the time the reference is executed.
- The execution of a function reference must not alter the value of any other entity within the same statement.
- The execution of a function reference must not alter the value of any entity that affects the value of any other function reference in the same statement.
Sun Microsystems, Inc.
Copyright information. All rights reserved.
|Library | Contents | Previous | Next | Index|