Control Flow in ABAP
Learn conditionals, loops, and branching in ABAP - including IF/ELSEIF, CASE/WHEN, DO, WHILE, LOOP AT, and the modern COND expression
Control flow is how a program decides what to do next - branching on conditions, repeating actions, and iterating over collections. ABAP, despite its enterprise pedigree and 1983 origins, provides a familiar set of structured control flow statements: IF/ELSEIF/ELSE, CASE/WHEN, DO, WHILE, and LOOP AT for internal tables.
What makes ABAP’s control flow distinctive is its tight integration with the system field sy-subrc (return code) and sy-index (loop iteration counter), which are implicitly set by many statements. Loop bodies frequently check these system fields rather than capturing return values directly. Modern ABAP (7.40+) also adds expression-style constructs like COND and SWITCH that work inline as functional expressions, blurring the line between statements and expressions.
In this tutorial you’ll see every major control flow construct in a single runnable program. The example uses the open-abap transpiler to execute on Node.js, so no SAP system is required.
Conditionals: IF / ELSEIF / ELSE
ABAP uses IF, optional ELSEIF branches, an optional ELSE, and a closing ENDIF. Comparison operators come in both symbolic (=, <>, <, >, <=, >=) and textual (EQ, NE, LT, GT, LE, GE) forms - both styles are common in legacy code. Logical operators are AND, OR, and NOT.
Branching: CASE / WHEN
CASE evaluates an expression once and matches it against WHEN branches. WHEN OTHERS is the default case (similar to default in C-family languages). Unlike C, ABAP does not have fall-through - each WHEN is independent.
Loops: DO, WHILE, and LOOP AT
ABAP has three loop forms:
DO ... ENDDO- unconditional loop, often combined withTIMESto set a fixed count. Inside the loop,sy-indexholds the 1-based iteration counter.WHILE ... ENDWHILE- loop while a condition holds.LOOP AT ... ENDLOOP- iterate over an internal table (ABAP’s built-in collection type). This is the most common loop in real ABAP code.
EXIT leaves the current loop entirely. CONTINUE skips to the next iteration. CHECK is similar to CONTINUE but with an embedded condition: CHECK lv_x > 0. skips the iteration if the condition is false.
Modern: COND and SWITCH expressions
Since ABAP 7.40, the COND and SWITCH constructor operators provide expression-style conditionals that return a value - useful for assignments and inline parameters where a full IF/CASE block would be clumsy.
Putting It All Together
Create a file named control_flow.abap:
| |
Running with Docker
Run the program using the same open-abap transpiler pattern from the Hello World tutorial - this time pointing at control_flow.abap instead of hello.abap.
| |
The transpiler converts the ABAP source to JavaScript, then @abaplint/runtime executes it - reproducing ABAP’s list buffer output on stdout.
Expected Output
Grade: B
Wednesday
Iteration 1
Iteration 2
Iteration 3
Count: 1
Count: 2
Count: 3
Fruit: apple
Fruit: banana
Fruit: cherry
Kept: 1
Kept: 3
Both positive
7 is odd
Code B = Bravo
Notes on Behavior
A few details worth calling out:
- The first
WRITE(without/) goes on the first line of the list. Every subsequentWRITE /adds a new line. This is why theGrade: Bline has no leading newline. sy-indexis 1-based and is set automatically byDO,WHILE, andLOOP AT. It is reset to its outer value when a loop ends, so nested loops do not corrupt the outer counter.- In the
EXIT/CONTINUEloop, iteration 2 is skipped (CONTINUE) and iteration 4 terminates the loop (EXIT), leaving only iterations 1 and 3 in the output. CONDandSWITCHrequire the result type as a parameter (COND string( ... )). Without it, the compiler cannot infer the return type.MODis ABAP’s modulo operator.lv_n MOD 2returns the remainder.
Real-World ABAP Control Flow
In production SAP code, control flow is dominated by LOOP AT over internal tables - typically results from SELECT statements against the database. A typical pattern looks like:
| |
Here CHECK acts as an inline filter inside the loop - one of ABAP’s compact idioms.
Key Concepts
IF/ELSEIF/ELSE/ENDIF- standard conditional branching; comparison operators come in symbolic and textual forms.CASE/WHEN/WHEN OTHERS/ENDCASE- multi-way branching with no fall-through.DO ... ENDDO- unconditional loop; combine withTIMESfor a fixed count and usesy-indexfor the 1-based iteration counter.WHILE ... ENDWHILE- condition-controlled loop.LOOP AT ... ENDLOOP- the workhorse loop for iterating internal tables in real ABAP code.EXIT,CONTINUE,CHECK- leave a loop, skip an iteration, or skip an iteration when a condition fails.CONDandSWITCH- 7.40+ expression-style conditionals that return a value, usable inline in assignments.sy-indexandsy-subrc- system fields implicitly set by control flow statements; reading them is idiomatic ABAP.
Comments
Loading comments...
Leave a Comment