Conditionals and loops, the CHECK statement and its context-dependent behaviour, and the system fields that go with them.
Open contents
ABAP control flow resembles other languages, with a few statements of its own โ CHECK and EXIT in particular โ whose behaviour is worth knowing precisely.
IF
Conditionals
IF lv_amount > 10000.
lv_discount = 10.
ELSEIF lv_amount > 5000.
lv_discount = 5.
ELSE.
lv_discount = 0.
ENDIF.
IF lv_matnr IS INITIAL.
" not set
ENDIF.
IF lt_items IS NOT INITIAL.
" has rows
ENDIF.
Comparison operators
Operator
Alternative
Meaning
=
EQ
Equal
<>
NE
Not equal
<
LT
Less than
>
GT
Greater than
BETWEEN a AND b
โ
Within a range
IS INITIAL
โ
Equal to the type initial value
IN
โ
Matches a ranges table
CS
โ
Contains string
CP
โ
Contains pattern
CASE
Branching on one value
CASE lv_status.
WHEN 'A'. lv_text = 'Received'.
WHEN 'B' OR 'C'. lv_text = 'In progress'.
WHEN 'Z'. lv_text = 'Complete'.
WHEN OTHERS. lv_text = 'Unknown'.
ENDCASE.
DO and WHILE
Counted and conditional loops
DO 10 TIMES.
WRITE: / sy-index.
ENDDO.
DO.
IF lv_count > 100. EXIT. ENDIF.
lv_count = lv_count + 1.
ENDDO.
WHILE lv_flag = abap_true.
" ...
ENDWHILE.
LOOP over internal tables
Looping
LOOP AT lt_items INTO DATA(ls_item).
WRITE: / ls_item-matnr.
ENDLOOP.
LOOP AT lt_items ASSIGNING FIELD-SYMBOL(<fs_item>).
<fs_item>-menge = <fs_item>-menge * 2.
ENDLOOP.
LOOP AT lt_items INTO ls_item WHERE menge > 100.
ENDLOOP.
Loop control
Statement
Behaviour
EXIT
Leave the loop entirely
CONTINUE
Skip to the next iteration
CHECK cond
If the condition is false, behave like CONTINUE
System fields
Field
Content
sy-subrc
Return code of the preceding statement; 0 means success