SAP Tutorials Blog


 


DATA(num) = 2.                    "integer

DATA(empty_string) = ``.          "empty string

DATA(flag) = 'x'.

DATA(dref) = NEW string( `ref` ). "data reference variable


"Object reference variable

DATA oref TYPE REF TO object.

"Creating an object and assigning it to the reference variable

oref = NEW zcl_demo_abap_prog_flow_logic( ).


"Declaration of and assignment to a field symbol

FIELD-SYMBOLS <fs> TYPE string.

ASSIGN `hallo` TO <fs>.


"Creating an internal table of type string inline

DATA(str_table) = VALUE string_table( ( `a` ) ( `b` ) ( `c` ) ).


"The following IF statement includes multiple expressions combined by AND to demonstrate different options


"Comparisons

IF 2 = num    "equal, alternative EQ

AND 1 <> num  "not equal, alternative NE

AND 1 < num   "less than, alternative LT

AND 3 > num   "greater than, alternative GT

AND 2 >= num  "greater equal, alternative GE

AND 2 <= num  "less equal, alternative LE


"Checks whether the content of an operand is within a closed interval

AND num BETWEEN 1 AND 3

AND NOT num BETWEEN 5 AND 7   "NOT negates a logical expression

AND ( num >= 1 AND num <= 3 ) "Equivalent to 'num BETWEEN 1 AND 3';

                              "here, demonstrating the use of parentheses


"Comparison operators CO, CN ,CA, NA, CS, NS, CP, NP for character-like data types;

"see the cheat sheet on string processing


"Predicate Expressions

AND empty_string IS INITIAL  "Checks whether the operand is initial. The expression

                             "is true, if the operand contains its type-dependent initial value

AND num IS NOT INITIAL       "NOT negates


AND dref IS BOUND  "Checks whether a data reference variable contains a valid reference and

                   "can be dereferenced;

                   "Negation (IS NOT BOUND) is possible which is also valid for the following examples

AND oref IS BOUND  "Checks whether an object reference variable contains a valid reference


"IS INSTANCE OF checks whether for a

"a) non-initial object reference variable the dynamic type

"b) for an initial object reference variable the static type

"is more specific or equal to a comparison type.

AND oref IS INSTANCE OF zcl_demo_abap_prog_flow_logic

AND oref IS INSTANCE OF if_oo_adt_classrun


AND <fs> IS ASSIGNED  "Checks whether a memory area is assigned to a field symbol


"See the predicate expression IS SUPPLIED in the executable example.

"It is available in method implementations and checks whether a formal parameter

"of a procedure is filled or requested.


"Predicate function: Some examples

AND contains( val = <fs> pcre = `\D` )  "Checks whether a certain value is contained;

                                        "the example uses the pcre parameter for regular expressions;

                                        "it checks whether there is any non-digit character contained

AND matches( val = <fs> pcre = `ha.+` ) "Compares a search range of the argument for the val parameter;

                                        "the example uses the pcre parameter for regular expressions;

                                        "it checks whether the value matches the pattern 'ha'

                                        "and a sequence of any characters


"Predicate functions for table-like arguments

"Checks whether a line of an internal table specified in the table expression

"exists and returns the corresponding truth value.

AND line_exists( str_table[ 2 ] )


"Predicative method call

"The result of the relational expression is true if the result of the functional method call

"is not initial and false if it is initial. The data type of the result of the functional method call,

"i. e. the return value of the called function method, is arbitrary.

"A check is made for the type-dependent initial value.

AND check_is_supplied( )

"It is basically the short form of such a predicate expression:

AND check_is_supplied( ) IS NOT INITIAL


"Boolean Functions

"Determine the truth value of a logical expression specified as an argument;

"the return value has a data type dependent on the function and expresses

"the truth value of the logical expression with a value of this type.


"Function boolc: Returns a single-character character string of the type string.

"If the logical expression is true, X is returned. False: A blank is returned.

"Not to be compared with the constants abap_true and abap_false in relational expressions,

"since the latter convert from c to string and ignore any blanks. Note: If the logical

"expression is false, the result of boolc does not meet the condition IS INITIAL since

"a blank and no empty string is returned. If this is desired, the function xsdbool

"can be used instead of boolc.

AND boolc( check_is_supplied( ) ) = `X`


"Result has the same ABAP type as abap_bool.

AND xsdbool( check_is_supplied( ) ) = abap_true


"Examples for possible operands


"Data objects as shown in the examples above

AND 2 = 2

AND num = 2


"Built-in functions

AND to_upper( flag ) = 'X'

AND NOT to_lower( flag ) = 'X'


"Numeric functions

AND ipow( base = num exp = 2 ) = 4


"Functional methods

"Method with exactly one return value

AND addition( num1 = 1 num2 = 1 ) = 2


"Calculation expressions

AND 4 - 3 + 1 = num


"String expressions

AND `ha` && `llo` = <fs>


"Constructor expression

AND CONV i( '2.03' ) = num

AND VALUE string_table( ( `a` ) ( `b` ) ( `c` ) ) = str_table


"Table expression

AND str_table[ 2 ] = `b`.


  "All of the logical expressions are true.


ELSE.


  "At least one of the logical expressions is false.


ENDIF.


Serkan AKKAVAK

Computer Engineer BSc

SAP Department Manager

Contact : serkurumsal@yandex.com