The DO statement in ABAP is a loop construct used to execute a block of statements
repeatedly. It comes mainly in two variants:
- Fixed number of iterations: Executes the loop block exactly
<n>times. - 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 hereENDDO.<n>: A numeric expression (literal, variable, constant - usually typeI) 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 anEXITstatement (orRETURNin 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.
- The value of
<n>is determined. sy-indexis set to 1.- As long as
sy-indexis less than or equal to<n>: a. The statement block is executed. b.sy-indexis automatically incremented by 1. - When
sy-indexis greater than<n>, the loop ends and execution continues afterENDDO.
DO.
sy-indexis set to 1.- The statement block is executed.
sy-indexis automatically incremented by 1.- Control always jumps back to the beginning of the statement block (after
DO.). - The loop is only exited by an explicit
EXITstatement (orRETURNetc.).
Control Statements within DO
EXIT: Terminates the entireDOloop (whetherDO n TIMESorDO.) immediately. Execution continues afterENDDO.CONTINUE: Skips the rest of the current iteration and jumps to the beginning of the next iteration (incrementssy-index, checks theTIMEScondition if applicable).CHECK <condition>: If<condition>is false, the rest of the current iteration is skipped (behaves likeCONTINUE).
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: Hello2. 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: 5Iteration: 2 | Add: 10 | Sum: 15Iteration: 3 | Add: 15 | Sum: 30Iteration: 4 | Add: 20 | Sum: 50-> Maximum reached or exceeded. EXIT.Loop ended. Final sum: 503. 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> TIMESwhen you know exactly how many times an action should be repeated. - Be extremely careful with the
DO.syntax withoutTIMES. Make absolutely sure there is a reachableEXITcondition, otherwise you will create an infinite loop! sy-indexis your friend withinDOloops to track the current iteration.- Choose the loop construct (
DO,WHILE,LOOP AT) that most clearly and efficiently represents the logic of your problem.