以 PDF 格式下载本书 (662 KB)
Chapter 3 ExpressionsThis chapter discusses Fortran expressions and how they are evaluated. Expressions, Operators, and OperandsAn 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:
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:
Arithmetic ExpressionsAn arithmetic expression evaluates to a single arithmetic value, and its operands have the following types. @ indicates a nonstandard feature.
The operators for an arithmetic expression are any of the following: Table 3-1 Arithmetic Operators
If BYTE or LOGICAL operands are combined with arithmetic operators, they are interpreted as integer data. Each of these operators is a binary operator in an expression of the form: a b 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: b where b is an operand, and is either of the - or + operators. -Z +B Basic Arithmetic ExpressionsEach arithmetic operator is shown in its basic expression in the following table: Table 3-2 Arithmetic Expressions
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
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. @ Example: 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 ModeIf 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:
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. Example of mixed mode: If R is real, and I is integer, then the expression: R * I has the type real, because first I is promoted to real, and then the multiplication is performed. RulesNote these rules for the data type of an expression:
Resultant TypeFor 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. Arithmetic AssignmentThe arithmetic assignment statement assigns a value to a variable, array element, or record field. The syntax is:
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. @
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 Fortran User's Guide for a description of these options. Example: Arithmetic assignment: INTEGER I2*2, J2*2, I4*4 LOGICAL L1, L2 REAL R4*4, R16*16 DOUBLE PRECISION DP COMPLEX C8, C16*16 J2 = 29002 I2 = J2 I4 = (I2 * 2) + 1 DP = 6.4D0 QP = 9.8Q1 R4 = DP R16 = QP C8 = R1 C8 = ( 3.0, 5.0 ) I2 = C8 C16 = C8 C8 = L1 R4 = L2 Character ExpressionsA 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, //.
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. The operands can be any of the following kinds of data items:
Examples: Character expressions, assuming C, S, and R.C are characters: 'wxy' 'AB' // 'wxy' C C // S C(4:7) R.C Note the following (nonstandard) exceptions:@
Character String AssignmentThe form of the character string assignment is:
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.
Example: The following program below displays joinedDD: 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)
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 AssignmentHere are the rules for character assignments:
Logical ExpressionsA 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
The period delimiters are necessary. Two logical operators cannot appear consecutively, unless the second one is the .NOT. operator. Logical operators are evaluated according to the following precedence: Table 3-5 Logical Operator Precedence
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
The following table shows the meanings of simple expressions: Table 3-7 Logical Expressions and Their Meanings
This is the syntax for the assignment of the value of a logical expression to a logical variable:
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. @ Example: A logical assignment: LOGICAL B1*1, B2*1 LOGICAL L3, L4 B2 = B1 B1 = L3 L4 = .TRUE. Relational OperatorA 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
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. 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 STR1 and STR2 are character type S .EQ. 'a' S is character type For character relational expressions:
Constant ExpressionsA 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 * 80
VOLUME = V*10**3
DO I = 1, 20*3
There are a few restrictions on constant expressions:
Record AssignmentThe general form of record assignment is: @
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 ID CHARACTER*16 NAME CHARACTER*8 MODEL REAL*4 COST REAL*4 PRICE END STRUCTURE RECORD /PRODUCT/ CURRENT, PRIOR, NEXT, LINE(10) ... CURRENT = NEXT LINE(1) = CURRENT WRITE ( 9 ) CURRENT NEXT.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 WRITE statement writes a whole record; and the last statement sets the ID of one record to 82. Evaluation of ExpressionsThe following restrictions apply to all arithmetic, character, relational, and logical expressions:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||