



Relational Expressions IF statements, as in this example:
IF(SENSOR .GT. UPPER) THEN CALL COOL ELSE IF(SENSOR .LT. LOWER) THEN CALL HEAT END IF |
.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' |
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:
|
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.
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:
< B < C etc.
< 1 < 2 etc.
'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:
|
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.
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.