2.3.3 Relational Expressions

A relational expression consists of two arithmetic expressions or two character expressions separated by a relational operator. A relational operator tests for a relationship between the two expressions. The value of the relational expression is either .TRUE. or .FALSE. depending on whether the stated relationship holds.

Fortran supports the following relational operators:

Operator  Relationship 
.LT.  Less than 
.LE.  Less than or equal to 
.EQ.  Equal to 
.NE.  Not equal to 
.GT.  Greater than 
.GE.  Greater than or equal to 

Both delimiting periods are required.

Complex expressions can be related only by the .EQ. and .NE. operators. Complex entities are equal if their corresponding real and imaginary parts are both equal.

In an arithmetic relational expression, the arithmetic expressions are first evaluated to obtain their values. These values are then compared to determine whether the relationship stated by the operator is valid; for example:

APPLE+PEACH .GT. PEAR+ORANGE

This expression states the relationship, "The sum of APPLE and PEACH is greater than the sum of PEAR and ORANGE." If this relationship is valid, the value of the expression is .TRUE. If not, the value of the expression is .FALSE.

Similarly, in a character relational expression, the character expressions are first evaluated to obtain their values. These values are then compared to determine whether the relationship stated by the operator is valid. In character relational expressions "less than" means "precedes in the ASCII collating sequence," and "greater than" means "follows in the ASCII collating sequence;" for example:

'AB'//'ZZZ' .LT. 'CCCCC'

This expression states that ' ABZZZ' is less than ' CCCCC'. Because that relationship is valid, the value of the expression is .TRUE. If the relationship stated is not valid, the value of the expression is .FALSE.

If the two character expressions in a relational expression are not the same length, the shorter one is padded on the right with spaces until the lengths are equal; for example:

'ABC' .EQ. 'ABC  '

'AB' .LT. 'C'

The first relational expression has a value of .TRUE. even though the lengths of the expressions are not equal, and the second has a value of .TRUE. even though ' AB' is longer than ' C'.

All relational operators have the same precedence. However, arithmetic and character operators have a higher precedence than relational operators.

As in any other expression, you can use parentheses to alter the order of evaluation of the expressions in a relational expression. However, because arithmetic and character operators are evaluated before relational operators, you do not need to enclose the entire arithmetic or character expression in parentheses.

A relational expression can compare two numeric expressions of different data types. In this case, the value of the expression with the lower-ranked data type is converted to the higher-ranked data type before the comparison is made.


Previous Page Next Page Table of Contents