LOOP - Loops on an internal table
Basic form
LOOP AT itab.
LOOP AT itab INTO wa.
Additions
1. ... FROM n1
2. ... TO n2
3. ... WHERE logexp
4. ... TRANSPORTING NO FIELDS
Effect
Processes an internal table
(DATA ) in a loop which begins with
LOOP and ends with ENDLOOP . Each of the internal table
entries is sent to the output area in turn.
When LOOP AT itab. is used, the header line of the internal
table itab is used as output area. In the case of LOOP AT
itab INTO wa , there is an explicitly specified work area wa .
If the internal table is empty, all the statements between LOOP
and ENDLOOP are ignored.
In each loop pass, SY-TABIX contains the index of the current
table entry. After leaving a LOOP , SY-TABIX has the same
value as it had before.
Inserting and/or deleting lines in a
LOOP affects subsequent loop passes.
For control break processing in a LOOP on internal tables, there
are special control break control structures for internal tables
you can use.
You can use the CONTINUE statement
to leave the current loop pass prematurely and continue with the next
loop pass. To leave loop processing altogether, you use
EXIT .
At the end of loop processing (i.e. after ENDLOOP ), the return
code value of SY-SUBRC specifies whether the loop was actually
processed.
SY-SUBRC = 0
The loop was executed at least once.
SY_SUBRC = 4
The loop was not executed, either because there was
no entry at all or because there was no entry which satisfied the
conditions.
Example
The table T is defined as follows:
-
DATA: BEGIN OF T OCCURS 100,
BAREA(2), BLNCE TYPE P,
END OF T.
After the table has been filled with data (using
APPEND ), it is then output:
-
LOOP AT T.
WRITE: / T-BAREA, T-BLNCE.
ENDLOOP.
Notes
If an internal table is processed only on a restricted
basis (with the additions FROM , TO and /or WHERE ),
you should not use the control structures for control break processing
because the interaction of a restricted LOOP
and the AT statement is undefined at present.
If SUM is used in a LOOP and an
explicit output area wa has also been specified, this output
area must be compatible with the line type of the internal table
itab .
Addition 1
... FROM n1
Addition 2
... TO n2
Effect
Places all internal table entries from the entry with
the index ( SY-TABIX ) = n1 to the entry with the index =
n2 inclusive in the output area in turn.
Note
If either one of the additions " FROM n1 " or " TO
n2 " is missing, then the table is processed either from the first
entry or up to the last entry (according to what is missing).
Example
Output table entries 7 and 8:
-
DATA: BEGIN OF T OCCURS 100,
BAREA(5), BLNCE(5),
END OF T.
LOOP AT T FROM 7 TO 8.
WRITE: / T-BAREA, T-BLNCE.
ENDLOOP.
Addition 3
... WHERE logexp
Effect
Places all internal table entries which satisfy the
condition logexp in turn in the output area. The condition
logexp can be almost any logical expression .
The only restriction is that the first field for each comparison must
be a sub-field of the line structure of the internal table itab .
Example
-
DATA: BEGIN OF T OCCURS 100,
BAREA(5), BLNCE(5),
END OF T.
LOOP AT T WHERE BAREA > 0.
WRITE: / T-BAREA, T-BLNCE.
ENDLOOP.
which has the same effect as:
-
LOOP AT T.
CHECK T-BAREA > 0.
WRITE: / T-BAREA, T-BLNCE.
ENDLOOP.
Notes
The interaction between the LOOP AT ... WHERE
statement and the AT control break statements
is currently undefined. It is therefore important to avoid using either
the AT NEW/END OF or FIRST/LAST statements in a
LOOP loop with a WHERE condition.
The performance of a LOOP AT ... WHERE statement can be
improved significantly if the fields to be compared always have the
same data type. The comparison fields should be defined as follows:
DATA LIKE .
Example
-
DATA: BEGIN OF T OCCURS 100,
BAREA(5), BLNCE(5),
END OF T.
DATA CMP_BAREA LIKE T-BAREA.
CMP_BAREA = '01'.
LOOP AT T WHERE BAREA = CMP_BAREA.
WRITE: / T-BAREA, T-BLNCE.
ENDLOOP.
Addition 4
... TRANSPORTING NO FIELDS
Effect
There is no field transport in the output area of the
internal table. This addition can be used only in conjunction with a
WHERE condition. Since it would make no sense to specify a work
area with INTO wa when using the addition TRANSPORTING NO
FIELDS , this option does not exist.
This addition can be used to determine a set of line indexes (index
set) or to determine the number of lines in a table which satisfy a
given condition.
Example
Determining the number COUNT of lines in a name
table TAB which contain the name 'Walter' and the corresponding
index set INDEX_SET .
-
DATA: BEGIN OF TAB OCCURS 100,
NAME(30) TYPE C,
END OF TAB,
COUNT TYPE I,
INDEX_SET LIKE SY-TABIX OCCURS 10 WITH HEADER LINE.
LOOP AT TAB TRANSPORTING NO FIELDS WHERE NAME CS 'Walter'.
INDEX_SET = SY-TABIX.
APPEND INDEX_SET.
ADD 1 TO COUNT.
ENDLOOP.
Related
Loop structures:
DO , WHILE
Table processing:
APPEND ,
COLLECT ,
INSERT ,
MODIFY ,
DELETE ,
SORT ,
AT NEW/END OF/FIRST/LAST ,
READ TABLE .
Index
© SAP AG 1996