Conditional Branching: Implementation
Conditional branching is a control structure that allows you to make the execution of code dependent on logical conditions.
The most common conditional branching consists of a pair of keywords IF and ENDIF. The ABAP code block between IF and ENDIF is only executed if the condition after IF is fulfilled.
You can add more code blocks extending the IF . . ENIF structure by one by using the keyword ELSE and an arbitrary number of blocks using the keyword ELSEIF. By adding the keyword ELSE, you ensure that always exactly one of the ABAP code blocks is executed. If ELSE is missing, then it is possible that none of the ABAP code blocks is executed.
Every IF . . ENDIF keywords are mandatory, and every IF must have an ENDIF.
The ABAP code block that is executed is as follows:
- First, the IF condition is evaluated. If it is fulfilled, the related ABAP code block is executed, and the program continues after the ENDIF.
Only if the IF condition is not fulfilled, the condition after the first ELSEIF is evaluated. If it is fulfilled, the related code block is executed, and the program continues after ENDIF.
This is done consecutively for all ELSEIF conditions. If none of the conditions are fulfilled and the structure contains ELSE, the ABAP code block after ELSE is executed. Otherwise, none of the ABAP code blocks are executed.
Logical conditions are a combination of comparisons, logical operations, expressions, and functions that the runtime system evaluates to decide whether the condition is true or false.
The most common use-case for logical conditions is after the keywords IF or ELSEIF in and IF . . . ENDIF structure.
The first example (in the figure above) is a simple comparison: the condition is true if the two data objects x and y have the same value.
The second example (in the figure above) is a bit more sophisticated: either the value of x is greater than or equal to y and less than twice the value of y or it is less than or equal to y and greater than twice the value of y.
The third example (in the figure above) makes use of arithmetic function abs( ) and logical expression BETWEEN <expression 1> AND <expression 2>. The condition is true if the absolute value of x lies between the absolute value of y and the absolute value of two times y.
For simple value comparisons you can use operators =, <>, >, <, >= and <=. You can not only compare the values of data objects, but the values of many other expressions like the arithmetic expressions 2 * y in the example above.
Operators and Meanings
Used to join multiple logical expressions to create a new logical expression that is true only when all of the multiple logical expressions are true.
Used to join multiple logical expressions to create a new logical expression which is true if at least one of the logical expressions is true.
The negation of a logical expression using
You can use operators AND and OR to combine logical expressions and operator NOT to negate an expression. Without brackets, NOT binds stronger than AND and AND stronger than OR.
ABAP knows some special logical expressions:
- <data object> IS INITIAL is true if <data object> contains its type-specific value
- <data object> is NOT INITIAL is true if <data object> contains a value that is different from the type-specific initial value
- <data object> BETWEEN <expression 1> AND <expression 2>
Some special ABAP functions are predicate functions. This means that they are logical conditions themselves. Contains( ) is a function that compares character-like values and line_exists ( ) performs an existence check for a row in an internal table.