ABAP DO Loop: Syntax, Variants and Usage of sy-index

Category
ABAP-Statements
Published
Author
Johannes

The DO statement in ABAP is a loop construct used to execute a block of statements repeatedly. It comes mainly in two variants:

  1. Fixed number of iterations: Executes the loop block exactly <n> times.
  2. Conditional exit from within: Executes the loop block potentially infinitely until the loop is explicitly exited by a statement like EXIT.

A useful system field within DO loops is sy-index, which automatically counts the current loop iteration (starting at 1).

Syntax

1. Fixed Number of Iterations (DO n TIMES)

DO <n> TIMES.
" Statement block
[Statement1].
[Statement2].
...
" Control statements like CONTINUE, EXIT, CHECK are possible here
ENDDO.
  • <n>: A numeric expression (literal, variable, constant - usually type I) that specifies the number of desired iterations. If <n> is less than or equal to zero, the statement block is not executed at all.
  • sy-index: Contains the current iteration number in each pass (1, 2, 3, …, n).

2. Loop with Exit from Within (DO.)

DO.
" Statement block
[Statement1].
...
IF <exit_condition>.
EXIT. " Absolutely necessary!
ENDIF.
...
ENDDO.
  • This variant has no built-in exit condition. It theoretically runs infinitely.
  • It is mandatory to include a condition (IF) with an EXIT statement (or RETURN in procedures) within the statement block to leave the loop and prevent an infinite loop.
  • sy-index: Also counts the iterations here (1, 2, 3, …).

How It Works

DO <n> TIMES.

  1. The value of <n> is determined.
  2. sy-index is set to 1.
  3. As long as sy-index is less than or equal to <n>: a. The statement block is executed. b. sy-index is automatically incremented by 1.
  4. When sy-index is greater than <n>, the loop ends and execution continues after ENDDO.

DO.

  1. sy-index is set to 1.
  2. The statement block is executed.
  3. sy-index is automatically incremented by 1.
  4. Control always jumps back to the beginning of the statement block (after DO.).
  5. The loop is only exited by an explicit EXIT statement (or RETURN etc.).

Control Statements within DO

  • EXIT: Terminates the entire DO loop (whether DO n TIMES or DO.) immediately. Execution continues after ENDDO.
  • CONTINUE: Skips the rest of the current iteration and jumps to the beginning of the next iteration (increments sy-index, checks the TIMES condition if applicable).
  • CHECK <condition>: If <condition> is false, the rest of the current iteration is skipped (behaves like CONTINUE).

Distinction from WHILE and LOOP AT

  • WHILE <condition>: Checks the condition before each iteration (head-controlled). Suitable when the number of iterations is not fixed but depends on a condition.
  • LOOP AT: Specifically designed for iterating over the rows of an internal table.
  • DO <n> TIMES: Ideal when the number of repetitions is known in advance (counter loop).
  • DO.: Rather rare; for cases where the exit condition is complex and best checked within the loop, or when multiple exit points exist.

Examples

1. DO n TIMES - Simple Repetition with sy-index

DATA message TYPE string VALUE 'Hello'.
DO 4 TIMES.
WRITE: / sy-index, '. Repetition:', message.
ENDDO.

Output:

1 . Repetition: Hello
2 . Repetition: Hello
3 . Repetition: Hello
4 . Repetition: Hello

2. DO. with EXIT (Summation up to a Limit)

DATA sum TYPE i VALUE 0.
DATA addend TYPE i VALUE 0.
DATA maximum TYPE i VALUE 50.
DO.
addend = addend + 5.
sum = sum + addend.
WRITE: / 'Iteration:', sy-index, ' | Add:', addend, ' | Sum:', sum.
IF sum >= maximum.
WRITE: / '-> Maximum reached or exceeded. EXIT.'.
EXIT. " Exit loop
ENDIF.
IF sy-index > 10. " Safety exit
WRITE: / '-> Safety limit (10 iterations) reached. EXIT.'.
EXIT.
ENDIF.
ENDDO.
WRITE: / 'Loop ended. Final sum:', sum.

Possible output:

Iteration: 1 | Add: 5 | Sum: 5
Iteration: 2 | Add: 10 | Sum: 15
Iteration: 3 | Add: 15 | Sum: 30
Iteration: 4 | Add: 20 | Sum: 50
-> Maximum reached or exceeded. EXIT.
Loop ended. Final sum: 50

3. DO n TIMES with CONTINUE (Skip Certain Iterations)

DO 6 TIMES.
IF sy-index MOD 2 = 0. " If sy-index is even...
WRITE: / 'Iteration', sy-index, 'skipped (even number).'.
CONTINUE. " ...go to next iteration
ENDIF.
WRITE: / 'Iteration', sy-index, 'is processed (odd number).'.
ENDDO.

Output:

Iteration 1 is processed (odd number).
Iteration 2 skipped (even number).
Iteration 3 is processed (odd number).
Iteration 4 skipped (even number).
Iteration 5 is processed (odd number).
Iteration 6 skipped (even number).

Important Notes / Best Practice

  • Use DO <n> TIMES when you know exactly how many times an action should be repeated.
  • Be extremely careful with the DO. syntax without TIMES. Make absolutely sure there is a reachable EXIT condition, otherwise you will create an infinite loop!
  • sy-index is your friend within DO loops to track the current iteration.
  • Choose the loop construct (DO, WHILE, LOOP AT) that most clearly and efficiently represents the logic of your problem.