Conditionals and loops, the CHECK statement and its context-dependent behaviour, and the system fields that go with them.
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
sy-index
Iteration counter in DO and WHILE
sy-tabix
Row index in LOOP
sy-datum / sy-uzeit
Current date and time
sy-uname
Logged-on user
sy-mandt
Client
sy-langu
Logon language
Check your understanding
Test what you just read.
Quiz 1
Which expression checks whether a variable holds its initial value in ABAP?
Quiz 2
The LOOP AT statement is a control structure for processing each row of an internal table sequentially.
Quiz 3
Which statement skips the current iteration and proceeds to the next iteration in an ABAP loop?
Quiz 4
Which comparison operator means "not equal" in ABAP?
Quiz 5
The DO~ENDDO statement repeats execution while a condition is true.
Quiz 6
Which control structure is most appropriate for branching based on comparison with multiple fixed values?
Hear when the paid version launches
A paid version is in preparation, with no ads, saved learning progress and many more practice questions. Leave your email and you will be told when it launches.
Your email address is used only to announce the paid version.