Entering content frameLoops Locate the document in its SAP Library structure

In a loop, a statement block is executed several times in succession. There are four kinds of loops in ABAP:

This section deals with DO and WHILE loops. SELECT is an Open SQL statement, and is described in the Open SQL section. The LOOP statement is described in the sections on internal tables and extract datasets.

Unconditional Loops

To process a statement block several times unconditionally, use the following control structure:

DO [<n> TIMES] [VARYING <f> FROM <f1> NEXT <f 2>].
  <Statement block>
ENDDO.

If you do not specify any additions, the statement block is repeated until it reaches a termination statement such as EXIT or STOP (see below). The system field SY-INDEX contains the number of loop passes, including the current loop pass.

Use the TIMES addition to restrict the number of loop passes to <n>. <n> can be literal or a variable. If <n> is 0 or negative, the system does not process the loop. If you do not use the TIMES option, you must ensure that the loop contains at least one EXIT or STOP statement to avoid endless loops.

You can assign new values to a variable <f> in each loop pass by using the VARYING option. You can use the VARYING addition more than once in a DO statement. <f 1 > and <f 2 > are the first two fields of a sequence of fields the same distance apart in memory and with the same type and length. In the first loop pass, <f> takes the value <f 1 >, in the second loop pass, <f 2 >, and so on. If you change the value of the field <f> within the DO loop, the value of the current field <f i > is also changed. You must ensure that there are not more loop passes than fields in the sequence, otherwise a runtime error occurs.

You can nest DO loops and combine them with other loop forms.

Example

Simple example of a DO loop:

DO.

WRITE SY-INDEX.
  IF SY-INDEX = 3.
    EXIT.
  ENDIF.
ENDDO.

The output is:

         1          2          3

The loop is processed three times. Here, the processing passes through the loop three times and then leaves it after the EXIT statement.

Example

Example of two nested loops with the TIMES addition:

DO 2 TIMES.
  WRITE SY-INDEX.
  SKIP.
  DO 3 TIMES.
    WRITE SY-INDEX.
  ENDDO.
  SKIP.
ENDDO.

The output is:

         1
         1          2          3
         2
         1          2          3

The outer loop is processed twice. Each time the outer loop is processed, the inner loop is processed three times. Note that the system field SY-INDEX contains the number of loop passes for each loop individually.

Example

Example of the VARYING addition in a DO loop:

DATA: BEGIN OF TEXT,
        WORD1(4) VALUE 'This',
        WORD2(4) VALUE 'is',
        WORD3(4) VALUE 'a',
        WORD4(4) VALUE 'loop',
      END OF TEXT.

DATA: STRING1(4), STRING2(4).

DO 4 TIMES VARYING STRING1 FROM TEXT-WORD1 NEXT TEXT-WORD2.
  WRITE STRING1.
  IF STRING1 = 'is'.
    STRING1 = 'was'.
  ENDIF.
ENDDO.

SKIP.

DO 2 TIMES VARYING STRING1 FROM TEXT-WORD1 NEXT TEXT-WORD3
VARYING STRING2 FROM TEXT-WORD2 NEXT TEXT-WORD4.
  WRITE: STRING1, STRING2.
ENDDO.

The output is:

This is a loop

This was a loop

The structure TEXT represents a series of four equidistant fields in memory. Each time the first DO loop is processed, its components are assigned one by one to STRING1. Whenever STRING1 contains ‘is’, its contents are changed to ‘was’. This also changes TEXT-WORD2 to ‘was’. Each time the second DO loop is processed, the components of TEXT are passed to STRING1 and to STRING2.

Conditional Loops

To repeat a statement block for as long as a certain condition is true, use the following control structure:

WHILE <condition> [VARY <f> FROM <f1> NEXT <f 2>].
   <statement block>
ENDWHILE.

<condition> can be any logical expression. The statement block between WHILE and ENDWHILE is repeated as long as the condition is true or until a termination statement such as EXIT or STOP occurs. The system field SY-INDEX contains the number of loop passes, including the current loop pass. The VARY option of the WHILE statement works in the same way as the VARYING option of the DO statement (see above).

To avoid endless loops, you must ensure that the condition of a WHILE statement can be false, or that the statement block contains a termination statement such as EXIT or STOP.

You can nest WHILE loops to any depth, and combine them with other loop forms.

Example

DATA: LENGTH     TYPE I VALUE 0,
      STRL       TYPE I VALUE 0,
      STRING(30) TYPE C VALUE 'Test String'.

STRL = STRLEN( STRING ).

WHILE STRING NE SPACE.
  WRITE STRING(1).
  LENGTH = SY-INDEX.
  SHIFT STRING.
ENDWHILE.

WRITE: / 'STRLEN:          ', STRL.
WRITE: / 'Length of string:', LENGTH.

The output appears as follows:

T e s t   S t r i n g
STRLEN:                    11
Length of String:          11

Here, a WHILE loop is used to determine the length of a character string. This is done by shifting the string one position to the left each time the loop is processed until it contains only blanks. This example has been chosen to demonstrate the WHILE statement. Of course, you can determine the length of the string far more easily and efficiently using the STRLEN function.

Terminating Loops

ABAP contains termination statements that allow you to terminate a loop prematurely. There are two categories of termination statement - those that only apply to the loop, and those that apply to the entire processing block in which the loop occurs. The STOP and REJECT statements belong to the latter group, and are described in more detail under Leaving Event Blocks.

The termination statements that apply only to the loop in which they occur are CONTINUE, CHECK, and EXIT. You can only use the CONTINUE statement in a loop. CHECK and EXIT, on the other hand, are context-sensitive. Within a loop, they only apply to the execution of the loop itself. Outside of a loop, they terminate the entire processing block in which they occur (subroutine, dialog module, event block, and so on).

CONTINUE, CHECK, and EXIT can be used in all four loop types in ABAP (DO, WHILE, LOOP, and SELECT).

Terminating a Loop Pass Unconditionally

To terminate a single loop pass immediately and unconditionally, use the CONTINUE statement in the statement block of the loop.

After the statement, the system ignores any remaining statements in the current statement block, and starts the next loop pass.

Example

DO 4 TIMES.
  IF SY-INDEX = 2.
    CONTINUE.
  ENDIF.
  WRITE SY-INDEX.
ENDDO.

The output is:

         1          3          4

The second loop pass is terminated without the WRITE statement being processed.

Terminating a Loop Pass Conditionally

To terminate a single loop pass conditionally, use the CHECK <condition> statement in the statement block of the loop.

If the condition is not true, any remaining statements in the current statement block after the CHECK statement are ignored, and the next loop pass starts. <condition> can be any logical expression.

Example

DO 4 TIMES.
  CHECK SY-INDEX BETWEEN 2 and 3.
  WRITE SY-INDEX.
ENDDO.

The output is:

         2          3

The first and fourth loop passes are terminated without the WRITE statement being processed, because SY-INDEX is not between 2 and 3.

Exiting a Loop

To terminate an entire loop immediately and unconditionally, use the EXIT statement in the statement block of the loop.

After this statement, the loop is terminated, and processing resumes after the closing statement of the loop structure (ENDDO, ENDWHILE, ENDLOOP, ENDSELECT). In nested loops, only the current loop is terminated.

Example

DO 4 TIMES.
  IF SY-INDEX = 3.
    EXIT.
  ENDIF.
  WRITE SY-INDEX.
ENDDO.

The output is:

         1          2

In the third loop pass, the loop is terminated before the WRITE statement is processed.

 

 

 

Leaving content frame