
A 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 code block between IF and ENDIF is only executed, if the condition after IF is fulfilled.
You can add more code blocks by extending the IF … ENDIF structure with up to one keyword ELSE and an arbitrary number of keywords ELSEIF. By adding keyword ELSE you ensure that always exactly one of the code blocks is executed. If ELSE is missing it can happen that none of the code blocks is executed.
The code block to be executed is determined as follows:
- First, the IF condition is evaluated. If it is fulfilled, the related code block is executed and the program continues after 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 is fulfilled and the structure contains ELSE, the code block after ELSE is executed. Otherwise the none of the code blocks is executed.
Hint
As opposed to many other programming languages, ABAP requires a delimiter (.) after each of the logical conditions and even after keyword ELSE.
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 keywords IF or ELSEIF in an IF ... ENDIF. structure.
The first example is a simple comparison: The condition is true if the two data objects x and y have the same value.
The second example 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 makes use of arithmetic function abs( ) and logical expression BETWEEN <expression1> AND <expression2> . 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 expression 2 * y in the example.
Note
ABAP uses the same symbol (=) for value assignments and for value comparisons. The distinction is made based on the position.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 initial 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 <expression1> AND <expression2>
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.

A second technique for conditional branching is the CASE … WHEN .. ENDCASE control structure.
Conditional branching with CASE .. ENDCASE is a special case of the more general branching with IF … ENDIF. You can use CASE in situations where the branching depends on the value of a single data object, which you consecutively compare to a set of possible values, using an equals comparison each time.
In the example, the value of data object number is compared to values 1 and 2. If the value equals 1, <code_block_1> is executed and if the value euqals 2, <code_blocl_2> is executed instead. For any other value, the code block after WHEN OTHERS is executed.
Any conditional branching with CASE … ENDCASE could be implemented with an IF … ENDIF structure, as well. This is illustrated with the example on the right.
Hint
You should use CASE … ENDCASE when dealing with the special case to increase readability of your code.