Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Program Flow Logic →  Control Structures →  Loops 

DO

Quick Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


DO [n TIMES]. 
  [statement_block]
ENDDO.

Addition

... n TIMES

Effect

Unconditional loop. The statements DO and ENDDO define a control structure, which can contain a closed statement block statement_block.

Without the addition n TIMES, the statement block is repeated until it is exited using one for the statements for leaving loops. In particular, the statement EXIT is ideal for exiting a loop completely. Within the statement block, the system field sy-index contains the number of previous loop passes, including the current pass. In nested loops, sy-index always refers to the current loop.


Notes

  • If the addition n TIMES is not specified, the loop has to be terminated by a statement; otherwise the loop is processed endlessly. A maximum runtime can be configured using the profile parameters rdisp/scheduler/prio_low/max_runtime, rdisp/scheduler/prio_normal/max_runtime, and rdisp/scheduler/prio_high/max_runtime. If this is exceeded, the program is ended by the runtime environment. The profile parameter rdisp/max_wprun_time, on the other hand, is obsolete.
  • If DO loops are used to construct values or fill internal tables, they can probably be expressed more elegantly using conditional iterations with FOR in constructor expressions.
  • The obsolete addition VARYING can be used to process a sequence of data objects in the memory.

Example

Exits a DO loop based on a condition.

DO.
  ...
  IF ...
    EXIT.
  ENDIF.
ENDDO.

Addition

... n TIMES

Effect

The addition n TIMES limits the amount of loop passes in a DO loop. n is a numeric expression position of operand type i.

The number value of n when entering the loop determines the maximum amount of passes of the statement block. The control structure ignores changes to the value n within the loop. If n contains a value less than or equal to 0, the statement block is not executed.


Example

Calculates and displays the first ten square numbers in a DO loop.

DATA square TYPE i. 

DO 10 TIMES. 
  square = ipow( base = sy-index exp = 2 ). 
  cl_demo_output=>write( |{ sy-index } { square }| ). 
ENDDO. 
cl_demo_output=>display( ). 

Continue

ENDDO