Conditional Branching: Implementation
Conditional branching is a control structure that allows you to make code execution 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 . . ENDIF 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 exactly one of the ABAP code blocks is always executed. If ELSE is missing, none of the ABAP code blocks may be 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 fulfilled, the related ABAP code block is executed, and the program continues after the ENDIF.
Only if the IF condition is not fulfilled is the condition after the first ELSEIF evaluated. If 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.
Hint
As opposed to many other programming languages, ABAP requires a delimiter (.) after each logical condition and even after the keyword ELSE.Logical Conditions
Logical conditions are a combination of comparisons, logical operations, expressions, and functions 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 an 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 the 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 compare the values of not only data objects but also many other expressions, like the arithmetic expression 2 * y in the example above.
Note
ABAP uses the same symbol (=) for value assignments and value comparisons. The distinction is made based on the position.Operators and Meanings
Operator | Meaning |
---|---|
AND | 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. |
OR | Used to join multiple logical expressions to create a new logical expression that is true if at least one of the logical expressions is true. |
NOT | The negation of a logical expression using NOT creates a new logical expression that is false if the logical expression is true and vice versa. |
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. For example, Contains( ) is a function that compares character-like values.