ABAP Core
ABAP Developmentbeginner

ABAP Core Part 4: Control Flow and Building Output Texts

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.

Explanation

📌 KEY POINTS
✅ Branches: IF/ELSEIF/ELSE and CASE/WHEN/WHEN OTHERS.

✅ Loops: LOOP AT itab (tables), DO n TIMES, WHILE condition; leave with EXIT, skip with CONTINUE or CHECK.

✅ Texts are built with CONCATENATE … INTO … SEPARATED BY space, formatted with WRITE … TO and cleaned with CONDENSE.

⚠️ CHECK inside a loop skips to the next iteration; outside a loop it leaves the processing block.

━━━━━━━━━━━━━━━━━━━━
▶ WHERE WE ARE

Parts 2–3 declared data. Now the program makes decisions, repeats work and produces readable text.

━━━━━━━━━━━━━━━━━━━━
▶ 1. IF AND CASE

IF lv_free = 0. … ELSEIF lv_free < 10. … ELSE. … ENDIF.

Comparison: =, <>, <, >, <=, >=, BETWEEN, IN (ranges), IS INITIAL, IS NOT INITIAL.

Logical: AND, OR, NOT — use parentheses for clarity.

CASE lv_carrid. WHEN 'LH'. … WHEN 'AA' OR 'UA'. … WHEN OTHERS. … ENDCASE.

━━━━━━━━━━━━━━━━━━━━
▶ 2. LOOPS

  • LOOP 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-index is the counter.
  • WHILE lv_x < 100. … ENDWHILE. — condition-based.
  • EXIT leaves the loop; CONTINUE jumps to the next iteration; CHECK cond skips the iteration if cond is false.

⚠️ DO without TIMES needs an EXIT, otherwise it never ends.

━━━━━━━━━━━━━━━━━━━━
▶ 3. BUILDING TEXTS

  • CONCATENATE lv_a lv_b INTO lv_text SEPARATED BY space.
  • WRITE lv_date TO lv_char (user date format), WRITE lv_amount TO lv_char CURRENCY lv_waers.
  • CONDENSE lv_text — remove extra blanks; SHIFT, TRANSLATE … TO UPPER CASE.
  • Leading zeros: CONVERSION_EXIT_ALPHA_OUTPUT (e.g. connection 0400 → 400).

━━━━━━━━━━━━━━━━━━━━
▶ 4. MODERN SYNTAX (SIDE NOTE)

From 7.40: COND/SWITCH assign values by condition in one expression, and string templates |Flight { lv_connid }| replace CONCATENATE. Useful to read in newer code; the classic statements do the same job.

━━━━━━━━━━━━━━━━━━━━
▶ STEP BY STEP

  1. Loop over flights of an airline and skip past dates with CONTINUE.
  2. Classify each flight with IF/ELSEIF into FULL / ALMOST FULL / AVAILABLE.
  3. Build an output line with WRITE … TO and CONCATENATE.
  4. React per status with CASE.

✅ CHECKPOINT: you can branch, loop and produce readable text with classic statements.

Code example

ABAP Code
REPORT zabap_core_flow. PARAMETERS p_carr TYPE s_carr_id DEFAULT 'LH'. DATA: lt_flights TYPE STANDARD TABLE OF sflight,      ls_f       TYPE sflight,      lv_free    TYPE i,      lv_status  TYPE c LENGTH 12,      lv_airline TYPE c LENGTH 20,      lv_connid  TYPE c LENGTH 4,      lv_date    TYPE c LENGTH 10,      lv_free_c  TYPE c LENGTH 6,      lv_line    TYPE c LENGTH 120. START-OF-SELECTION.  SELECT * FROM sflight INTO TABLE lt_flights    UP TO 15 ROWS    WHERE carrid = p_carr.  SORT lt_flights BY fldate.   LOOP AT lt_flights INTO ls_f.* ===== skip past flights =====    IF ls_f-fldate < sy-datum.      CONTINUE.    ENDIF.     lv_free = ls_f-seatsmax - ls_f-seatsocc. * ===== IF / ELSEIF classification =====    IF lv_free <= 0.      lv_status = 'FULL'.    ELSEIF lv_free < 10.      lv_status = 'ALMOST FULL'.    ELSE.      lv_status = 'AVAILABLE'.    ENDIF. * ===== CASE on a single value =====    CASE ls_f-carrid.      WHEN 'LH'.        lv_airline = 'Lufthansa'.      WHEN 'AA'.        lv_airline = 'American Airlines'.      WHEN OTHERS.        lv_airline = ls_f-carrid.    ENDCASE. * ===== build the text: conversion exit, WRITE TO, CONCATENATE =====    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'      EXPORTING input  = ls_f-connid      IMPORTING output = lv_connid.               " 0400 -> 400    WRITE ls_f-fldate TO lv_date.                 " user date format    WRITE lv_free TO lv_free_c.    CONDENSE lv_free_c.    CONCATENATE lv_airline lv_connid 'on' lv_date ':' lv_free_c 'free' lv_status      INTO lv_line SEPARATED BY space.    WRITE: / lv_line. * ===== CASE for actions per status =====    CASE lv_status.      WHEN 'FULL'.        WRITE: / '   -> put customers on the waiting list'.      WHEN 'ALMOST FULL'.        WRITE: / '   -> notify sales team'.      WHEN OTHERS.        " nothing to do    ENDCASE.  ENDLOOP. * ===== DO with counter and EXIT =====  DO.    IF sy-index > 3.      EXIT.                           " always provide an exit for DO without TIMES    ENDIF.    WRITE: / 'Reminder', sy-index, 'sent'.  ENDDO. * 💡 Modern syntax (7.40+):*    DATA(lv_st) = COND string( WHEN lv_free <= 0 THEN `FULL` ELSE `AVAILABLE` ).*    DATA(lv_txt) = |{ lv_airline } { ls_f-connid ALPHA = OUT } on { ls_f-fldate DATE = USER }|.

Real project scenario

A 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, dates appeared in the wrong format and amounts had the wrong decimals for JPY.
Replacing the code with string templates using DATE = USER and CURRENCY = currency fixed both issues without special handling per country, and halved the code size.

Common mistakes

  • DO loops without EXIT.
  • Long IF chains where COND or CASE is clearer.
  • Formatting dates and amounts manually with offsets.
  • Using CHECK outside loops without knowing it leaves the block.
  • Forgetting WHEN OTHERS in CASE.

Best practices

  • Use COND/SWITCH for value assignments, IF/CASE for actions.
  • Keep loop bodies short; move logic into methods.
  • Use string templates with formatting options.
  • Always provide an exit condition.
  • Use WHERE in LOOP AT instead of IF inside the loop when possible.

Interview angle

Questions: "Difference between CONTINUE, EXIT and CHECK?", "What are COND and SWITCH?", "How do string templates format dates and currencies?"
Strong answer: CONTINUE skips to the next iteration, EXIT leaves the loop, CHECK skips when false (inside loops) or leaves the block; COND evaluates conditions, SWITCH compares one operand, both return values; templates embed expressions in { } with options like DATE = USER, CURRENCY =, ALPHA = OUT, and && chaining.

Practice this lesson

Quick check

1. Which string template prints a date in the user's format?

2. String template |{ x DATE = USER }| does what?

3. What does the CASE statement in ABAP compare with?

Interview questions

1. When do you use IF, CASE, COND and SWITCH in ABAP?

Answer it in your own words first, then compare.

2. Explain string templates in ABAP and how they help avoid classic string concatenation bugs.

Answer it in your own words first, then compare.

Practice more with Interview AI →

Reference-verified: the steps are checked against official SAP documentation and tutorials, and object and field names in the sample code are checked for consistency. The code has not been compiled or run on an SAP system by ERPClimb. Syntax marked release-dependent varies by SAP release, so check it in your own system.

More lessons in this topic

Interview questions on this topic

Related SAP topics