ERPClimb logoERPClimb
ABAP Core
ABAP Developmentbeginner 8 min read

ABAP Core Part 4: Control Flow and Building Output Texts

Preview · a free account unlocks the full lesson

Key takeaway

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 sections

Visual overview

ABAP Core Part 4: Control Flow and Building Output Texts — learning sheetFlow: Loop over flights of an airline and skip past dates with CONTINUE → Classify each flight with IF/ELSEIF into FULL / ALMOST FULL / AVAILABLE → Build an output line with WRITE … TO and CONCATENATE → React per status with CASE. Key ideas: IF AND CASE; LOOPS; BUILDING TEXTS; MODERN SYNTAX (SIDE NOTE). Watch out: CHECK inside a loop skips to the next iteration; outside a loop it leaves the processing block.ERPClimb.com / ABAP / LESSON 4/10 / LEARNING SHEETABAP Core Part 4: Control Flow and Building Output Texts1 · HOW IT FLOWS1Loop overflights of anairline and skip…2Classify eachflight withIF/ELSEIF into…3Build an outputline with WRITE… TO and…4React per statuswith CASE2 · KEY IDEAS EXPLAINEDIf And CaseIF lv_free = 0. … ELSEIF lv_free < 10. … ELSE.… ENDIF.LoopsLOOP AT lt_flights INTO ls_flight WHERE carrid ='LH'. … ENDLOOP. — main loop for internal tables(details in Internal Tables).DO 5 TIMES. … ENDDO. — fixed repetitions; sy-indexis the counter.Building TextsCONCATENATE lv_a lv_b INTO lv_text SEPARATED BYspace.WRITE lv_date TO lv_char (user date format), WRITElv_amount TO lv_char CURRENCY lv_waers.Modern Syntax (Side Note)From 7.40: COND/SWITCH assign values by conditionin one expression, and string templates |Flight {lv_connid }| replace CONCATENATE. Useful to read…3 · REMEMBER · WATCH OUTRememberBranches: IF/ELSEIF/ELSE and CASE/WHEN/WHEN OTHERS.Loops: LOOP AT itab (tables), DO n TIMES, WHILE condition; leave withEXIT, skip with CONTINUE or CHECK.Texts are built with CONCATENATE … INTO … SEPARATED BY space,formatted with WRITE … TO and cleaned with CONDENSE.!Watch outCHECK inside a loop skips to thenext iteration; outside a loop itleaves the processing block.4 · IN A REAL PROJECTA billing report built its output lines with nested CONCATENATE statements and manual date formatting("DD.MM.YYYY" built with offsets). When the company rolled the report out to US users, datesappeared in the wrong format and amounts had the wrong decimals for JPY. Replacing the code with string…Original ERPClimb learning sheet • Verify against your SAP release and edition

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.

Free account

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.