Professional Documents
Culture Documents
Logical Ex Continued
Logical Ex Continued
Use the following logical operators to compare two data objects with different data types: <operator> EQ = NE <> >< LT < LE <= GT > GE >= Both operands must either be compatible or convertible. Meaning equal to equal to not equal to not equal to not equal to less than less than less than or equal to less than or equal to greater than greater than greater than or equal to greater than or equal to
IF HEX2 > HEX1. ... ENDIF. The logical expression in the IF statement is true, since the first byte of HEX2 is greater than the first byte of HEX1. When you compare incompatible data objects whose data types are different, the system performs a type conversion before the comparison according to the following rules:
1. If one of the operands is a floating point number (type F), the system also converts the other
operands to type F. 2. If one of the operands is a packed field (type P), the system also converts the other operands to type P. 3. If one of the operands is a date field (type D) or a time field (type T), the system converts the other operands to type D or T respectively. Comparisons between date and time fields are not supported, and lead to a program termination. 4. If one of the operands is a character field (type C) and the other operand is a hexadecimal field (type X), the system converts the operand of type X to type C. 5. If one of the operands is a character field (type C) and the other a numeric field (type N), the system converts both operands to type P.
DATA: NUM(4) TYPE N VALUE '1234', TEXT(5) TYPE C VALUE '567.8'. IF NUM > TEXT. ... ENDIF. The logical expression in the IF statement is true, since the value of NUM is greater than the value of TEXT after the conversion to packed numbers. If NUM had had type C, the logical expression would have been false, since there would have been no conversion to a packed number.
Comparing References
When you compare compatible or convertible reference variables in ABAP Objects, it only makes sense to use the equality (EQ or =) and inequality (NE, <>, or ><) operators. The equality comparison is true if both reference variables contain references to the same object. It is false if they contain references to different objects. The converse is true if you are testing for inequality.
INTERFACE I1. ... ENDINTERFACE. CLASS C1 DEFINITION. PUBLIC SECTION. INTERFACES I1.
... ENDCLASS. DATA: R1 TYPE REF TO I1, R2 TYPE REF TO C1. CREATE OBJECT R2. R1 = R2. IF R1 = R2. ... ENDIF. The logical expression in the IF statement is true, since both references point to the same object.
Comparing Structures
Compatible structures are broken down into their elementary components, which are then compared. Two structures are equal if all of their elementary components are equal. If the structures are unequal, the first pair of elements that are unequal determine which structure is larger.
DATA: BEGIN OF STRUCT1, COL1 TYPE I VALUE 1, COL2 TYPE P DECIMALS 2 VALUE '56.78', END OF STRUCT1. DATA: BEGIN OF STRUCT2, COMP1 TYPE I VALUE 10, COMP2 TYPE P DECIMALS 2 VALUE '12.34', END OF STRUCT2. IF STRUCT1 < STRUCT2. ... ENDIF. The logical expression in the IF expression is true, since the value of the first component of STRUCT1 is less than the value of the second component. When you compare incompatible structures, or compare structures with elementary fields, the structures are converted into type C fields before the conversion, and then treated like elementary fields.
DATA: BEGIN OF STRUCT, COL1(5) TYPE C VALUE '12345', COL2(5) TYPE N VALUE '12345', END OF STRUCT.
DATA TEXT(10) TYPE C VALUE '1234512345'. IF STRUCT = TEXT. ... ENDIF. The logical expression in the IF expression is true, since structure STRUCT has the same contents as the field TEXT once it has been converted into a field with type C.