ABAP Keyword Documentation → ABAP - Reference → Program Flow → Control Structures → Loops
DO
Other versions: 7.31 | 7.40 | 7.54
Syntax
DO [n TIMES].
[statement_block]
ENDDO.
Effect
Unconditional loop. The DO
and ENDDO
statements define a control structure that can contain a closed statement_block
statement block.
Without the n TIMES
addition, the statement block is repeated until it is exited with one of the statements for
exiting loops. The DS:ABAP.EXIT_LOOP>EXIT
statement, in particular, is intended for exiting loops completely. Within the statement block, the
sy-index
system field contains the number of previous loop passes (including the current loop). In nested loops, sy-index
always refers to the current loop.
With the n TIMES
addition, you can limit the number of loop passes. n
is a
numerical expression position of operand type i
.
The numerical value that n
has at the point of entry into the loop determines
how often the statement block can be executed. The control structure does not take into account changes
to value n
within the loop. If n
contains a value less than or equal to 0, the statement block is not executed.
Note
If the n TIMES
addition is not specified, the loop must be exited with a statement; otherwise, the loop is processed endlessly. The
profile parameter rdisp/max_wprun_time limits the maximum execution time of an ABAP program.
-
The obsolete addition
VARYING
can be used to process a sequence of data objects in the memory.
Example
Calculation and output of the first ten square numbers in a DO
loop.
DATA square TYPE i.
DO 10 TIMES.
square = sy-index ** 2.
WRITE: / sy-index, square.
ENDDO.