Control Flow
ABAP programs are made of processing blocks: event blocks (START-OF-SELECTION…), dialog modules (MODULE), procedures (methods, function modules, subroutines).
9.1 IF / ELSEIF / ELSE
IF lv_amount > 10000.
lv_level = 'HIGH'.
ELSEIF lv_amount > 1000.
lv_level = 'MEDIUM'.
ELSE.
lv_level = 'LOW'.
ENDIF.9.2 CASE
CASE lv_status.
WHEN 'O'.
lv_text = 'Open'.
WHEN 'C' OR 'X'.
lv_text = 'Closed'.
WHEN OTHERS.
lv_text = 'Unknown'.
ENDCASE.
" Type-based CASE (7.50+)
CASE TYPE OF lo_shape.
WHEN TYPE zcl_circle INTO DATA(lo_circle).
lv_area = lo_circle->area( ).
WHEN OTHERS.
ENDCASE.9.3 Loops
DO n TIMES … ENDDO endless until EXIT WHILE cond … ENDWHILE LOOP AT itab … ENDLOOP internal table rows SELECT … ENDSELECT database rows (avoid; prefer INTO TABLE)
DO 5 TIMES.
WRITE: / 'Iteration', sy-index.
ENDDO.
DATA(lv_n) = 1.
WHILE lv_n < 1000.
lv_n *= 2.
ENDWHILE.9.4 Loop control
Outside a loop skip rest of iteration if false leaves the processing block (avoid outside loops) leaves the loop leaves the processing block leaves the processing block (method, form, event)
LOOP AT lt_orders INTO DATA(ls_order).
CHECK ls_order-status <> 'X'. " skip cancelled
IF ls_order-amount = 0.
CONTINUE.
ENDIF.
IF lv_total > 1000000.
EXIT.
ENDIF.
lv_total += ls_order-amount.
ENDLOOP.9.5 Processing blocks and events
ABAP programs are made of processing blocks: event blocks (START-OF-SELECTION…), dialog modules (MODULE), procedures (methods, function modules, subroutines). Statements outside any block belong to START-OF-SELECTION implicitly in reports.
9.6 Iteration expressions (preview)
Instead of explicit loops, modern ABAP builds data with FOR inside constructor expressions:
DATA(lt_squares) = VALUE int4_table( FOR i = 1 UNTIL i > 10 ( i * i ) ).🧪 Part I Exercises
Write a report that prints your name, the current date in ISO format, the time, the user name (sy-uname), and the system ID (sy-sysid).
Create a domain ZD_PRIORITY (CHAR 1, fixed values L/M/H), a data element using it, and a transparent table ZTASKS with key, title, priority, due date, and status.
Declare a structure type for an employee (ID, name, birth date, salary as packed number) and fill it; print the employee's age in years computed from sy-datum.
Using COND, convert a numeric score (0–100) into a grade; using SWITCH, convert a month number into its name.
Write a string template that prints an amount with currency formatting and a date in user format.
Given lv_csv = 'Apple;Banana;Cherry', split it into a table, convert each entry to upper case, and join it back with | .
Validate an e-mail address with a PCRE regular expression and print "valid"/"invalid".
Print the first 20 Fibonacci numbers with DO and then with WHILE.
Print all prime numbers below 200 using nested loops with CONTINUE and EXIT.
Create a message class with three messages containing placeholders, and output them as success, warning and error messages from a report.