Relational Expressions - Fortran

A relational expression yields a logical value of either .TRUE. or .FALSE. on evaluation and comparison of two arithmetic expressions or two character expressions. A relational expression can appear only within a logical expression.

Relational Operators
Table below lists the Fortran relational operators. Arithmetic and character operators are evaluated before relational operators.
Table: Fortran Relational Operators

Fortran Relational Operators.

Relational Operands
The operands of a relational operator can be arithmetic or character expressions. The relational expression requires exactly two operands and is written in the following form:

e1 relop e2
where
e1 and e2 are arithmetic or character expressions.
relop is the relational operator.

Note: Both e1 and e2 must be the same type of expression, either arithmetic or character.
The result of a relational expression is of type logical, with a value of .TRUE. or .FALSE.. The manner in which the expression is evaluated depends onthe data type of the operands.

Arithmetic Relational Expressions
In an arithmetic relational expression, e1 and e2 must each be an integer, real, double precision, complex, or double complex expression. relop must be a relational operator.

The following are examples of arithmetic relational expressions:

(a + b) .EQ. (c + 1)
HOURS .LE. 40

You can use complex type operands only when specifying either the .EQ. or .NE. relational operator.

An arithmetic relational expression has the logical value .TRUE. only if the values of the operands satisfy the relation specified by the operator.

Otherwise, the value is .FALSE..
If the two arithmetic expressions e1 and e2 differ in type, the expression is evaluated as follows:

((e1) - (e2)) relop 0

where the value 0 (zero) is of the same type as the expression ((e1)- (e2)) and the type conversion rules apply to the expression. Do not compare a double precision value with a complex value.

Character Relational Expressions
In a character relational expression, e1 and e2 are character expressions and relop is a relational operator.

The following is an example of a character relational expression:

NAME .EQ. 'HOMER'

A character relational expression has the logical value .TRUE. only if the values of the operands satisfy the relation specified by the operator. Otherwise, the value is .FALSE..

The result of a character relational expression depends on the collating sequence as follows:

  • If e1 and e2 are single characters, their relationship in the collating sequence determines the value of the operator. e1 is less than or greater than e2 if e1 is before or after e2, respectively, in the collating sequence.
  • If either e1 or e2 are character strings with lengths greater than 1, corresponding individual characters are compared from left to right until a relationship other than .EQ. can be determined.
  • If the operands are of unequal length, the shorter operand is extended on the right with blanks to the length of the longer operand for the comparison.
  • If no other relationship can be determined after the strings are exhausted, the strings are equal.

The collating sequence depends partially on the processor; however, equality tests .EQ. and .NE. do not depend on the processor collating sequence and can be used on any processor.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Fortran Topics