Summary -
In this topic, we described about the below sections -
Relational operators used to compare two or more operands of any data type. Relational operators and join two or more operands of any data type to form a relational expression or comparison expression. The result of the relational expression is true or false.
There are additional relational operators for specific data types. The predicate operator "IS" qualifying an operand. If operands are of different length and different type, then automatic conversion is performed.
Automatic type conversion is performed on either one of the operand. The automatic conversion can be decided by the data type and below list shows the data type order precedence.
- If one operand is of data type I, then the other operand is converted to I.
- If one operand is of data type P, then the other operand is converted to P.
- If one operand is of data type D, then the other operand is converted to D.
- C and N types are not converted and they are compared directly.
- If one operand is of data type N and the other operand is C or X, both operands are converted to P.
- If one operand is of data type C and the other operand is X, the X type is converted to C.
Relational operators for all data types -
Below are the list of relational operators for all data types -
Operator | Description | Meaning |
---|---|---|
=, EQ | Equal | True when the value of operand1 matches the value of operand2, otherwise false. |
<>, NE | Not Equal | True when the value of operand1 does not match the value of operand2, otherwise false. |
< LT | Less Than | True when the value of operand1 is less than the value of operand2, otherwise false. |
>, GT | Greater Than | True when the value of operand1 is greater than the value of operand2, otherwise false. |
<=, LE | Less Equal | True when the value of operand1 is less than or equal to the value of operand2, otherwise false. |
>=, GE | Greater Equal | True when the value of operand1 is greater than or equal to the value of operand2, otherwise false. |
IS INITIAL | INITIAL | The condition becomes true if the content of the variable not changes from the value initially assigned. |
IS NOT INITIAL | NOT INTIAL | The condition becomes true if the content of the variable changed from the value initially assigned. |
Relational operators for character-like data types -
Below are the list of relational operators for character-like data types -
Operator | Description | Meaning |
---|---|---|
CO | Contains Only | True when operand1 only contains characters from operand2. It is case-sensitive and trailing blanks are respected in both operands. |
CN | Contains Not Only | True when a logical expression with CO is false, that is, if operand1 contains not only characters from operand2. |
CA | Contains Any | True, when operand1 contains at least one character from operand2. It is case-sensitive and trailing blanks are respected in both operands. |
NA | Contains Not Any | True, when a logical expression with CA is false, that is if operand1 does not contain any characters from operand2. |
CS | Contains String | True, when the content of operand2 is contained in operand1. It is not case-sensitive and trailing blanks in the left operand are respected. |
NS | Contains No String | True, when a logical expression with CS is false, i.e., if operand1 does not contain the content of operand2. |
CP | Covers Pattern | True, when the content of operand1 fits the pattern in operand2. |
NP | No Pattern | True, when a logical expression with CP is false, i.e., if operand1 does not fit the pattern operand2. |
Relational operators for byte-like data types -
Below are the list of relational operators for byte-like data types -
Operator | Description | Meaning |
---|---|---|
BYTE-CO | Contains Only | True when operand1 only contains bytes out of operand2. |
BYTE-CN | Contains Not Only | True when a logical expression with BYTE-CO is false. |
BYTE-CA | Contains Any | True when operand1 contains at least one byte out of operand2. |
BYTE-NA | Contains Not Any | True when a logical expression with BYTE-CA is false. |
BYTE-CS | Contains String | True when the content of operand2 is contained in operand1. |
BYTE-NS | Contains No String | True when a logical expression with BYTE-CS is false. |
Example -
Below example shows how the relational operations coded in the program.
Code -
*&---------------------------------------------------------------------*
*& Report Z_RELATIONAL_OPERATORS
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*
REPORT Z_RELATIONAL_OPERATORS.
* Declaring variables
DATA: W_OP1 TYPE I VALUE 40,
W_OP2 TYPE I VALUE 60,
W_OP3 TYPE I.
* Verifying W_OP1 less than W_OP2
IF W_OP1 LT W_OP2.
WRITE 'W_OP1 LESS THAN W_OP2'.
ENDIF.
* Verifying W_OP2 IS NOT INITIAL
IF W_OP2 IS NOT INITIAL.
WRITE /'W_OP2 IS CHANGED'.
ENDIF.
* Verifying W_OP3 IS INITIAL
IF W_OP3 IS INITIAL.
WRITE /'W_OP3 IS NOT CHANGED'.
ENDIF.
Output -
Explaining Example -
In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.
IF W_OP1 LT W_OP2, verifying W_OP1 is LESS THAN (LT) W_OP2. If yes, display 'W_OP1 LESS THAN W_OP2'.
IF W_OP2 IS NOT INITIAL, verifying W_OP2 is not initial. If yes, displays 'W_OP2 IS CHANGED'.
IF W_OP3 IS INITIAL, verifying W_OP3 is initial. If yes, displays 'W_OP3 IS NOT CHANGED'.