Start of Content Area

Loops  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:

·        Unconditional loops using the DO statement.

·        Conditional loops using the WHILE statement.

·        Loops through internal tables and extract datasets using the LOOP statement.

·        Loops through datasets from database tables using the SELECT statement.

This section deals with DO and WHILE loops. SELECT is an Open SQL statement, and is described in the Open SQLsection. 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] ...
  [statement_block]
ENDDO.

Use the TIMES addition to restrict the number of loop passes to n.

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.

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 list 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 list 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.

Conditional Loops

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

WHILE log_exp
  [statemaent_block]
ENDWHILE.

log_exp 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.

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

Example

REPORT demo_flow_control_while.

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 STOPand REJECT statements belong to the latter group (see Exiting Eventblocks).

The termination statements that apply only to the loop in which they occur are CONTINUE, CHECKand 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 EXITcan 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.

CONTINUE.

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 list 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.

CHECK condition.

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 list 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.

EXIT.

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 list output is:

         1          2

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

 

 

 

 

End of Content Area