Summary -

In this topic, we described about the below sections -

LOOP Statement reads line by line into <work-area> or into the table work area. LOOP statement is another statement used for reading or processing the internal table.

The processing statements-block should be coded in between LOOP and ENDLOOP. For each read, the statements-block coded in between LOOP and ENDLOOP processed one time.

LOOP AT <itable> [INTO <work-area>] 
	[FROM <n1>] [TO <n2>] [WHERE <condition>].
...
ENDLOOP.

  • <itable> - Specifies the internal table.
  • <work-area> - Specifies the table work area.
  • FROM <n1> - Specifies the index of the first line to be read.
  • TO <n2> - Specifies the index of the last line to be read.
  • WHERE <condition> - Specifies the logical expression and the operand in the logical expression must be a component of <itable>.

SY-TABIX contains the index of the current retrived line once the LOOP statement gets executed. The loop processing ends as soon as all lines of the table have been processed.

SY-SUBRC = 0: If at least one line retrieved. Otherwise it is set to 4.

The FROM and TO options restrict the number of lines and specifies the number of lines that the system must read. The WHERE option filters the lines that satisfies the condition. Without the WHERE option, the system read all lines. So, it is always best practise of specifying FROM, TO or WHERE options to improve performance.


Example -

Simple example to read the data using LOOP statement from product information internal table with below structure.

Pid -Character (10)
Pname - Character (40)
Pamount - Decimal (10)


Code -

*&---------------------------------------------------------------------*
*& Report  Z_INTERNAL_TABLE
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*

REPORT  Z_INTERNAL_TABLE.

* internal table Structure creation
TYPES: BEGIN OF t_product,
       pid(10)     TYPE C,
       pname(40)   TYPE C,
       pamount(10) TYPE P,
       END OF t_product.

* Data & internal table declaration
DATA: wa TYPE t_product,
      it TYPE TABLE OF t_product,
      it1 TYPE TABLE OF t_product.

* inserting data to the internal table of INDEX 1
wa-pid     = 'IFB1'.
wa-pname   = 'IFB WASHING MACHINE'.
wa-pamount = 31000.
INSERT wa INTO TABLE it.

* inserting data to the internal table of INDEX 2
wa-pid     = 'IFB2'.
wa-pname   = 'IFB SPLIT AC'.
wa-pamount = 38000.
INSERT wa INTO TABLE it.

WRITE 'displaying IT table data...'.
* Reading internal table for all the records
LOOP AT it INTO wa.
  IF sy-subrc = 0.
    WRITE :/ wa-pid, wa-pname, wa-pamount.
  ELSE.
    WRITE 'No Record Found'.
  ENDIF.
ENDLOOP.

Output -

LOOP Statement Example Output

Explaining Example -

In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.

t_product - Structure declaration, wa - work area of type t_product and it - internal table declaration of type t_product.

INSERT wa INTO TABLE it - Inserting work area(wa) to the table(it) and LOOP AT it INTO wa - Loop to read the internal table to work area record by record.