SuivantPrec.Bas prec.BasNiv. sup.

7.6 Relational Expressions 

A relational expression compares the values of two arithmetic expressions or two character expressions: the result is a logical value, either true or false. Relational expressions are commonly used in IF statements, as in this example:
       IF(SENSOR .GT. UPPER) THEN
           CALL COOL
       ELSE IF(SENSOR .LT. LOWER) THEN
           CALL HEAT
       END IF
The relational operators have forms such as .GT. and .LT. because the Fortran character set does not include the usual characters . and <. Relational expressions are most commonly used in IF statements, but any logical variable or array element may be used to store a logical value for use later on.
       CHARACTER*10 OPTION
       LOGICAL EXIT
       EXIT = OPTION .EQ. 'FINISH'
 *...
       IF(EXIT) STOP 'Finish requested'
Logical expressions are covered in more detail in the next section.

General Forms of Relational Expression 

       arithmetic-exprn rel-op arithmetic-exprn or     character-exprn rel-op character-exprn In either case the resulting expression has the logical type. The relational operator rel-op can be any of the following:
.EQ. equal to
.GE. greater than or equal to
.GT. greater than
.LE. less than or equal to
.LT. less than
.NE. not equal to
Note that these operators need a decimal point at either end to distinguish them from symbolic names.

Arithmetic Comparisons 

When the two arithmetic values of differing data type are compared, a conversion is automatically applied to one of them (as in arithmetic expressions) to bring it to the type of the other. The direction of conversion is always: integer ===> real ===> complex or double precision. When comparing integer expressions, there is a considerable difference between the .LE. and .LT. operators, and similarly between .GE. and .GT., so that you should consider carefully what action is required in the limiting case before selecting the appropriate operator.

In comparisons involving the other arithmetic types you should remember that the value of a number may not be stored exactly. This means that it is unwise to rely on tests involving the .EQ. and .NE. operators except in special cases, for example if one of the values has previously been set to zero or some other small integer.

There are two restrictions on complex values: firstly they cannot be compared at all to ones of double precision type. Secondly they cannot use relational operators other than .EQ. and .NE. because there is no simple linear ordering of complex numbers.

Character comparisons 

A character value can only be compared to another character value; if they do not have the same length then the shorter one is padded out with blanks to the length of the other before the comparison takes place. Tests for equality (or inequality) do not depend on the character code, the two strings are just compared character by character until a difference is found. Comparisons using the other operators (.GE., .GT., .LE., and .LT.) do, however, depend on the local character code. The two expressions are compared one character position at a time until a difference is found: the result then depends on the relative positions of the two characters in the local collating sequence, i.e. the order in which the characters appear in the character code table. The Fortran Standard specifies that the collating sequence used by all systems must have the following basic properties:

It does not, however, specify whether letters precede digits or follow them. As a result, if strings of mixed text are sorted using relational operators the results may be machine dependent. For example, the expression       'APPLE' .LT. 'APRICOT' is always true because at the two strings first differ at the third character position, and the letter 'P' precedes 'R' in all Fortran collating sequences. However:       'A1' .GT. 'AONE' will have a value true if your system uses EBCDIC but false if it uses ASCII, because the digits follow letters in the former and precede them in the latter.

In order to allow character comparisons to be made in a truly portable way, Fortran has provided four additional intrinsic functions. These perform character comparisons using the ASCII collating sequence no matter what the native character code of the machine. These functions are:
LGE(S1, S2)greater than or equal to
LGT(S1, S2) greater than
LLE(S1, S2) less than or equal to
LLT(S1, S2) less than.
They take two character arguments (of any length) and return a logical value. Thus the expression:       LGT('A1', 'AONE') will always have the value false.

Character comparisons are case-sensitive on machines which have lower-case letters in their character set. It is advisable to convert both arguments to the same case beforehand.

Guidelines 

Systems which supports both upper and lower-case characters are usually case-sensitive: before testing for the presence of particular keywords or commands it is usually best to convert the an input string to a standard case, usually upper-case. Unfortunately there are no standard intrinsic functions to do this, though many systems provide them as an extension.

In character sorting operations where the strings contain mixtures of letters, digits, or other symbols, you should use the intrinsic functions to make the program portable. In other character comparisons, however, the relational operator notation is probably preferable because it has a more familiar form and may be slightly more efficient.

SuivantPrec.Bas prec.HautNiv. sup.