5. EXPRESSIONS AND ASSIGNMENT
5.3 RELATIONAL EXPRESSIONS
A relational expression compares the values of two arithmetic or character expressions, producing a logical value of true or false.
Relational expressions can appear within logical expressions. Relational operators are as follows:
Operator .LT.
.LE.
.EQ.
.NE.
.GE •
• GT.
Operation (Comparison) Less than
Less than or equal to Equal to
Not equal to
Greater than or equal to Greater than
Relational operators have no precedence within this group because the use of more than one operator in the same relational expression is illegal.
Table 5-5 shows which data types can be used together in relational operations. The result type is logical for all relational operations.
Table 5-5 • Data Types in Relational Operations:
• EQ.,.NE.,.GT.,.GE.,.LT.,LE.
xoz
z I R D C B L S
x
I L L L Ll L3
•
.2R L L L Ll L3
•
.2D L L L Ll
• • •
C L1 Ll L1
• • • •
B L3 L3
• •
L• •
L
• • • • • • •
S .2 .2
• • • •
LLegend:
Xoz Relational operation: .EQ.,.NE.,.GT.,.GE.,.LT.,LE.
• Prohibited I Integer B Boolean
R Real D Double-precision S Character string
C Complex L Loqical
1 Only .EQ. and .NE. are allowed for complex comparisons.
2 If S is a literal and length (S) ~ 8 characters, the operation is performed with S treated as a Hollerith constant; a warning message is issued. See the preceding heading, Character Expressions.
3 The comparison is performed without conversion of either operand.
5.3.1 ARITHMETIC RELATIONAL EXPRESSIONS
An arithmetic relational expression is a relational expression whose operands are arithm~tic expressions or arithmetic array expressions. An arithmetic relational expression is interpreted as the logical value TRUE if the values of the expressions satisfy the relation specified by the operator; FALSE if they do not. If the operands are arrays, an array of logical values is returned.
A complex expression is permitted only when the relational operator is .EQ. or .NE.
In the relational expression x relop y, if arithmetic expressions x and yare of different types, the expression is evaluated as (x-y) relop O. The expression in parentheses would be evaluated according to the type conventions for arithmetic expressions, as shown in table 5-3.
Examples:
A .LE. B
INDEX .EQ. ENDVALU
J(1,6,6)*COS(ALPHA/10.) .GT. Z 3.1415927 .LT. (22./7.)
CMPLXM .NE. CMPLXN
5.3.2 CHARACTER RELATIONAL EXPRESSIONS
A character relational expression is a relational expression in which both operands are character expressions. The result is interpreted as the logical value TRUE if the values of the operands satisfy the relation specified by the operator; otherwise, the result is interpreted as the logical value FALSE.
The character expression that comes first in the collating sequence (see Appendix A) is considered to be of lower value. If the operands are of unequal length, the shorter operand is extended on the right with blanks to the length of the longer operand.
5.4 LOGICAL EXPRESSIONS
A logical expression specifies a logical computation. Evaluation of a logical expression produces a result of type logical with a value of either TRUE or FALSE. Tables 5-6 and 5-7 show the logical operators
and their usage. Table 5-8 shows that operands for logical expressions are always of type logical; table 5-4 shows that only logical entities can be used in logical assignment statements.
5.4.1 LOGICAL ASSIGNMENT STATEMENT
Execution of a logical assignment statement causes the evaluation of logical expression Ie and the definition of logical entity Iv with the value of Ie.
Iv
=
IeIv Name of a logical variable, logical array, logical array section, or array element
Ie Logical expression Examples:
All variable and array element names are assumed to be of type logical except E and F, which are type real.
T
=
.FALSE.A
=
BC = (A .AND. B) .OR. (C .AND. D) T = .NOT. T
TRUTAB(I,J,K,L)
=
.T.T
=
E.GE.F .OR. ElF .LT • • 4 T=
A .EOV. B5.4.2 LOGICAL OPERATORS
The logical operators are shown in table 5-6; their use with operands is shown in table 5-7. The .NOT. or .N. operator produces the logical complement of its operand.
For logical expressions containing two or more logical operators, the precedence, as shown in table 5-6, determines the order in which they are to be combined (unless changed by the use of parentheses).
Example:
A .OR. B .AND. C
In the preceding expression, .AND. has higher precedence than .OR.
Therefore the expression is interpreted as follows:
A • OR • (B • AND • C )
Logical operators can also be written as functions; for example A.AND.B can be written as AND(A,B) • • NOT. is written as COMPL(), and .NOT •• OR.
is replaced by EQV(). See appendix B.
Table 5-6. Logical Operators
Operator
• NOT. or .N.
.AND. or .A.
.OR. or .0.
.XOR. or .X. or .NEQV.
.EQV.
Operation
Logical negation Logical conjunction
Logical inclusive disjunction Logical exclusive disjunction or logical nonequivalence Logical equivalence
Precedence
Highest
Lowest
The ANSI FORTRAN Standard does not provide for the .XOR. operator or for .N., .A., .0., or .X. as abbreviations.
Table 5-7. Meanings of Logical Operators
Xl.XOR.X2
Xl X2 Xl.AND.X2 Xl. ) R .X2 Xl.NEQV.X2 X1.EQV.X2
true true true true false true
true false false true true false
false true false true true false
false false false false false true
5.4.3 FORM AND INTERPRETATION OF LOGICAL EXPRESSIONS
A logical operand is an entity that can be operated on by a logical operator. Logical operands can be any of the following:
•
Logical primaries•
Logical factors•
Logical terms•
Logical disjuncts•
Logical expressionsA logical primary is a primary in a logical expression. Logical primaries can be any of the following:
• Logical constants
• Symbolic names of logical constants
• Logical variable or array element references
• Logical function references
• Relational expressions
• Logical expressions enclosed in parentheses
A logical factor consists of a logical primary with or without the .NOT. operator. The form of a logical factor is:
[.NOT.] logical-primary
A logical term is a sequence of logical factors separated by an .AND.
operator. If a logical term contains two or more .AND. operators, the logical factors are combined from left to right. The form of a logical term is:
[logical-term .AND.] logical-factor
A logical disjunct is a sequence of logical terms separated by an .OR.
operator. If a logical disjunct contains two or more .OR. operators, the logical terms are combined from left to right. The form of a logical disjunct is:
[logical-disjunct .OR.] logical-term
A logical expression is a sequence of logical disjuncts separated by .XOR., .EQV., or .NEQV. operators. If a logical expression contains two or more .XOR., .EQV., and/or .NEQV. operators, the logical disjuncts are combined from left to right. The forms of a logical expression are:
[logical expression .XOR.] logical disjunct [logical expression .EQV.] logical disjunct [logical expression .NEQV.] logical disjunct
These forms allow the logical operator .NOT. to immediately follow any other logical operator. For example, the following logical term is permitted:
LOGICALX .AND • • NOT. LOGICALY