# Relational Operators There are six relational operators:

• < : less than
• <= : less than or equal to
• > : greater than
• >= : greater than or equal to
• == : equal to
• /= : not equal to
Here are important rules:
• Each of these six relational operators takes two operands. These two operands must both be arithmetic or both be strings. For arithmetic operands, if they are of different types (i.e., one INTEGER and the other REAL), the INTEGER operand will be converted to REAL.
• The outcome of a comparison is a LOGICAL value. For example, 5 /= 3 is .TRUE. and 7 + 3 >= 20 is .FALSE.
• All relational operators have equal priority and are lower than those of arithmetics operators as shown in the table below:

 Type Operator Associativity Arithmetic ** right to left * / left to right + - left to right Relational < <= > >= == /= none

This means that a relational operator can be evaluated only if its two operands have been evaluated. For example, in

```a + b /= c*c + d*d
```
expressions a+b and c*c + d*d are evaluated before the relational operator /= is evaluated.
• If you are not comfortable in writing long relational expressions, use parenthesis. Thus,
```3.0*SQRT(Total)/(Account + Sum) - Sum*Sum >= Total*GNP - b*b
```
is equivalent to the following:
```(3.0*SQRT(Total)/(Account + Sum) - Sum*Sum) >= (Total*GNP - b*b)
```
• Although a < b < c is legal in mathematics, you cannot write comparisons this way in Fortran. The meaning of this expression is a < b and b < c. You should use logical operator to achieve this. ### Examples

• 3**2 + 4**2 == 5**2 is .TRUE.
• If the values of REAL variables a, b and c are 1.0, 2.0 and 4.0, respectively, then b*b - 4.0*a*c >= 0.0 is equivalent to 2.0*2.0 - 4.0*1.0*4.0 >= 0.0, which evaluates to -12.0 >= 0.0. Thus, the result is .FALSE.
• If REAL variables x and y have values 3.0 and 7.0, and INTEGER variables p and q have values 6 and 2, what is the result of x*x - y*y + 2.0*x*y /= p*q + p**3 - q**3?
```x*x - y*y + 2.0*x*y /= p*q + p**3 - q**3
--> 3.0*3.0 - 7.0*7.0 + 2.0*3.0*7.0 /= 6*2 + 6**3 - 2**3
--> [3.0*3.0] - 7.0*7.0 + 2.0*3.0*7.0 /= 6*2 + 6**3 - 2**3
--> 9.0 - 7.0*7.0 + 2.0*3.0*7.0 /= 6*2 + 6**3 - 2**3
--> 9.0 - [7.0*7.0] + 2.0*3.0*7.0 /= 6*2 + 6**3 - 2**3
--> 9.0 - 49.0 + 2.0*3.0*7.0 /= 6*2 + 6**3 - 2**3
--> [9.0 - 49.0] + 2.0*3.0*7.0 /= 6*2 + 6**3 - 2**3
--> -40.0 + 2.0*3.0*7.0 /= 6*2 + 6**3 - 2**3
--> -40.0 + [2.0*3.0]*7.0 /= 6*2 + 6**3 - 2**3
--> -40.0 + 6.0*7.0 /= 6*2 + 6**3 - 2**3
--> -40.0 + [6.0*7.0] /= 6*2 + 6**3 - 2**3
--> -40.0 + 42.0 /= 6*2 + 6**3 - 2**3
--> [-40.0 + 42.0] /= 6*2 + 6**3 - 2**3
--> 2.0 /= 6*2 + 6**3 - 2**3
--> 2.0 /= [6*2] + 6**3 - 2**3
--> 2.0 /= 12 + 6**3 - 2**3
--> 2.0 /= 12 + [6**3] - 2**3
--> 2.0 /= 12 + 216 - 2**3
--> 2.0 /= [12 + 216] - 2**3
--> 2.0 /= 228 - 2**3
--> 2.0 /= 228 - [2**3]
--> 2.0 /= 228 - 8
--> 2.0 /= [228 - 8]
--> 2.0 /= 
--> 2.0 /= 220.0
--> .TRUE.
```
In the above, please note the left-to-right evaluation order and the type conversion making 220 to 220.0 before carrying out /= ### Comparing CHARACTER Strings

Characters are encoded. Different standards (e.g. BCD, EBCDIC and ASCII) may have different encoding schemes. To write a program that can run on all different kind of computers and get the same comparison results, one can only assume the following ordering sequences:
```A < B < C < D < E < F < G < H < I < J < K < L < M
< N < O < P < Q < R < S < T < U < V < W < X < Y < Z

a < b < c < d < e < f < g < h < i < j < k < l < m
< n < o < p < q < r < s < t < u < v < w < x < y < z

0 < 1 < 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9
```
If you compare characters in different sequences such as 'A' < 'a' and '2' >= 'N', you are asking for trouble since different encoding methods may produce different answers. Moreover, do not assume there exists a specific order among upper and lower case letters, digits, and special symbols. Thus, '+' <= 'A', '*' >= '%', 'u' > '\$', and '8' >= '?' make no sense. However, you can always compare if two characters are equal or not equal. Hence, '*' /= '9', 'a' == 'B' and '8' == 'b' are perfectly fine.

Here is the method for comparing two strings:

• The comparison always starts at the first character and proceeds from left to right.
• If the two corresponding characters are equal, then proceed to the next pair of characters.
• Otherwise, the string containing the smaller character is considered to be the smaller one. And, the comparison halts.
• During the process comparison, if
• both strings have consumed all of their characters, they are equal since all of their corresponding characters are equal.
• otherwise, the shorter string is considered to be the smaller one.

### Examples

• Compare "abcdef" and "abcefg"
```a b c d e f
= = = <
a b c e f g
```
The first three characters of both strings are equal. Since 'd' of the first string is smaller than 'e' of the second, "abcdef" < "abcefg" holds.
• Compare "01357" and "013579"
```0 1 3 5 7
= = = = =
0 1 3 5 7 9
```
Since all compared characters are equal and the first string is shorter, "01357" < "013579" holds.
• What is the result of "DOG" < "FOX"?
```D O G
<
F O X
```
The first character (i.e., 'D' < 'F') determines the outcome. That is, "DOG" < "FOX" yields .TRUE. ### A Special Note

The priority of all six relational operators is lower than the string concatenation operator //. Therefore, if a relational expression involves //, then all string concatenations must be carried out before evaluating the comparison operator. Here is an example:
```"abcde" // "xyz" < "abc" // ("dex" // "ijk")
--> ["abcde" // "xyz"] < "abc" // ("dex" // "ijk")
--> "abcdexyz" < "abc" // ("dex" // "ijk")
--> "abcdexyz" < "abc" // (["dex" // "ijk"])
--> "abcdexyz" < "abc" // ("dexijk")
--> "abcdexyz" < "abc" // "dexijk"
--> "abcdexyz" < ["abc" // "dexijk"]
--> "abcdexyz" < "abcdexijk"
--> .FALSE.
```