Entering content frameSpecifying the Index in Loops Locate the document in its SAP Library structure

When you process an internal table in a loop, you can specify the index of an index table to restrict the number of entries that are read:

LOOP AT <itab> <result> [FROM <n1>] [TO <n 2>] <condition>.
  <statement block>
ENDLOOP.

The loop is processed as described in Processing Table Lines in Loops. Within the processing block, the system field SY-TABIX contains the index of the current line.

You can use the additions FROM and TO to specify the indexes <n 1 > and <n 2 > of the first and last entries that you want to read. The FROM and TO options restrict the number of lines which the system has to read. The WHERE addition in the condition only prevents the <result> from being processed. However, all of the table lines are read. To improve performance, you should use the FROM and TO options as much as possible. It can be also beneficial under certain conditions to leave the loop with the CONTINUE or EXIT statement.

Example

Example

DATA: BEGIN OF LINE,
         COL1 TYPE I,
         COL2 TYPE I,
      END OF LINE.

DATA ITAB LIKE SORTED TABLE OF LINE WITH UNIQUE KEY TABLE LINE.

DO 30 TIMES.
  LINE-COL1 = SY-INDEX.  
  LINE-COL2 = SY-INDEX ** 2.
  APPEND LINE TO ITAB.
ENDDO.

LOOP AT ITAB INTO LINE FROM 10 TO 25 WHERE COL2 > 400.
  WRITE: / SY-TABIX, LINE-COL2.
ENDLOOP.

The output is:

        21       441

        22       484

        23       529

        24       576

        25       625

The example fills a sorted table ITAB with 30 lines. In the loop, only the lines 10 to 25 are read. There is also a condition that the contents of COL2 must be more than 400.

 

 

 

 

Leaving content frame