Relational operators are used in simple expressions to compare two base terms of the same type. They are used in complex expressions to compare expressions, the results of which must have the same data type.
Comparison | Operators | ||
Less than | LT | < | |
Less than or equal to | LE |
| |
Equal to | EQ | = | == |
Not equal to | NE | <> | |
Greater than or equal to | GE | >= | |
Greater than | GT | > |
The result of a relational operation is always a Boolean value, i.e. a value that is either FALSE or TRUE.
The same rules apply to all relational operators; therefore, these rules are described only once.
Rules:
The operands of a relational operator must be of the same type; otherwise, an error message is issued and error handling is activated.
The result of a relational operation is either TRUE or FALSE.
If the relational operator is an equals sign (=), the relational expression must be enclosed in parentheses to distinguish a comparison of equality from the assignment of
a value to an operand (operand1 = operand2). If the equals sign is duplicated, the parentheses can be omitted.
Example
/B = A + COUNT /IF (B = A + COUNT)
The first line contains an assignment: variable B is assigned the results yielded by adding the contents of the variables A and COUNT.
The second line contains a relational comparison: if the contents of variable B, which were set at another position in the procedure, correspond to the results yielded by adding A and COUNT, the THEN branch of the IF block is executed. The contents of variable B are not modified, nor is it assigned a new value. In order to make the difference between an assignment and a comparison more clear, the relational operator can optionally be written as “==”:
/IF (B == A + COUNT)
Numeric comparison
A “numeric comparison” is when both operands of the relational operator are integer expressions. The values of the operands are compared.
Comparison of Boolean values
Both operands of the relational operator must be Boolean expressions.
Rules:
Only the following operators are allowed:
Operation | Operators | ||
Equal to | EQ | = | == |
Not equal to | NE | <> |
String comparison
“String comparison” means that both operands of the relational operator are string expressions.
Rules:
Strings are compared character by character (i.e. byte by byte), from left to right, until the first difference between characters is detected.
The first difference between characters determines which string is greater or less; the other characters are no longer taken into account for the comparison.
The terms “greater” and “less” are based on the order of the characters in EBCDI code, from X’00’ to X’FF’.
If the lengths of the two strings differ but have the same character string up to the last character of the shorter string, the shorter string is considered to be less.
Strings are equal if they are the same length and have exactly the same characters.
A character-by-character or byte-by-byte comparison means that the EBCDIC equivalents for the characters are examined.
Example
Assignment | Result |
/A = 'ABC' /B = 'ABCDE' /C = X'C1C2C3' /D = 'B' /E = (B > A) /F = (D > A) /G = (C = A) /H = (B = A) | TRUE TRUE TRUE FALSE |
Variable E is assigned the Boolean value TRUE, since the first three characters of the strings in variables B and A are identical but the string in variable A (’ABC’) is shorter and therefore less than the string in variable B (’ABCDE’).
Variable F is also assigned the Boolean value TRUE: string ’B’ (in variable D) is shorter that the string ’ABC’ (in variable A) but the first character in the string ’B’ has a higher value in EBCDI code than the first character in the string ’ABC’.
A comparison of the variable contents of C and A returns equality, since the X string with which the C variable was initialized is the half-byte notation for the string ’ABC’; consequently, variable G is assigned the Boolean value TRUE.
Variable H is assigned the Boolean value FALSE, since the strings in variables B and A are not equal.