ABAP Core Part 4: Control Flow and Building Output Texts
Preview · a free account unlocks the full lesson
Control program logic with IF, CASE, loops (DO, WHILE, LOOP AT), CHECK/CONTINUE/EXIT, and build readable output texts with CONCATENATE, WRITE … TO, CONDENSE and conversion exits — with COND, SWITCH and string templates as a modern side note.
In this lessonShow sectionsHide sections
Visual overview
Explanation
Parts 2 and 3 covered declaring data. Now the program needs to make decisions, repeat work, and produce readable text output.
Branching comes in two forms. IF/ELSEIF/ELSE handles conditions: IF lv_free = 0. … ELSEIF lv_free < 10. … ELSE. … ENDIF. Comparisons available include =, <>, <, >, <=, >=, BETWEEN, IN for ranges, and IS INITIAL / IS NOT INITIAL, while logical operators AND, OR and NOT can be combined — using parentheses to keep the logic clear. CASE/WHEN handles multi-way branches on a single value: CASE lv_carrid. WHEN 'LH'. … WHEN 'AA' OR 'UA'. … WHEN OTHERS. … ENDCASE.
Loops repeat work in a few different ways. LOOP AT lt_flights INTO ls_flight WHERE carrid = 'LH'. … ENDLOOP. is the main loop construct for internal tables, with full details covered in the Internal Tables topic. DO 5 TIMES. … ENDDO. repeats a fixed number of times, with sy-index giving the current counter.
WHILE lv_x < 100. … ENDWHILE. repeats based on a condition rather than a fixed count.
Within any of these, EXIT leaves the loop entirely, CONTINUE jumps straight to the next iteration, and CHECK cond skips the current iteration if cond is false — though CHECK behaves differently outside a loop, where it leaves the entire processing block instead of just skipping an iteration.
One trap worth watching for: a DO loop without TIMES needs its own EXIT, or it never ends.
Create a free account to keep reading
You have read the opening of this lesson. A free ERPClimb account unlocks the rest of it, every other lesson and topic, and your learning history, so you can pick up exactly where you stopped. No card needed.
- Full explanation
- Code example
- Real project scenario
- Common mistakes
- Best practices
- Interview angle
One tap with Google works too. Every topic keeps its first lesson open to everyone.
Keep exploring
Referenced SAP objects
Reviewed reference pages for the objects this page mentions.
Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.