LOOP AT itab.
LOOP
AT itab INTO wa.
LOOP AT itab ASSIGNING <fs>.
LOOP AT itab REFERENCE INTO dref.
1. ... FROM
n1
2. ...
TO n2
3. ...
TRANSPORTING NO FIELDS
4. ... WHERE logexp
The syntax check performed in an ABAP Objects context is stricter than in other ABAP areas. See Cannot Use Short Forms in Line Operations and Cannot Change an Internal Table in a Loop.
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 you use LOOP AT itab, the header line of the
internal table itab is used as the output area (this is
only possible for tables with a header line). When you use LOOP AT
itab INTO wa, the explicitly specified work area wa
is used as the output area. In both cases, the current table line is copied into the output area.
To
avoid the costs for the copy process, you can use the additions ASSIGNING
<fs> and REFERENCE INTO dref. In both
cases, the system directly processes the contents of the internal table. If you use the
ASSIGNING addition, the field symbol <fs>
points to the respective current line. If you use REFERENCE INTO
dref, the reference of the current line is put into dref.
Within the loop, neither the field symbol <fs> nor
the reference dref must be processed.
If the table
is empty, or if the additions prevent the system from choosing an entry, the output area (field symbol
or the reference variable) remains unchanged. At the end of the loop, the output area displays (or the field symbol or the reference points to) the last entry selected.
STANDARD TABLE:
The system processes entries according
to their logical index. At the beginning of a loop pass, SY-TABIX
is set to the logical index of the current table entry.
SORTED
TABLE:
The system processes entries according to their sort order. As with standard tables,
the system sets SY-TABIX to the logical index of the current
table entry.
HASHED TABLE:
The system
processes entries in the order in which they were inserted into the table. Since hashed tables have
no logical index, SY-TABIX is always set to 0.
When
the system exits a
LOOP, the index field SY-TABIX
always has the same value it had before the loop was processed.
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 (that is after ENDLOOP),
the return code value of SY-SUBRC specifies whether the loop was actually processed.
The statement LOOP AT also fills the system
fields SY-TFILL and SY-TLENG.
The table itab is defined as follows:
DATA: BEGIN OF STRUC,
NAME(10) TYPE C,
BLNCE TYPE
P,
END OF STRUC,
ITAB LIKE TABLE OF STRUC.
After the table has been filled with data (using APPEND), it is then output:
LOOP AT ITAB INTO STRUC.
WRITE: / STRUC-NAME, STRUC-SALDO.
ENDLOOP.
... FROM n1
... TO n2
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.
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).
Output table entries 7 and 8:
LOOP AT ITAB INTO STRUC FROM 7 TO 8.
WRITE: / STRUC-NAME, STRUC-BLNCE.
ENDLOOP.
... TRANSPORTING NO FIELDS
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.
Determining the number COUNT of lines in a name table
TAB which contain the name 'Walter' and the corresponding index set INDEX_SET.
TYPES: BEGIN OF NAMETAB_TYPE,
NAME(30) TYPE C,
END OF NAMETAB_TYPE.
DATA: NAMETAB TYPE
STANDARD TABLE OF NAMETAB_TYPE WITH
NON-UNIQUE DEFAULT KEY INITIAL SIZE 100,
COUNT TYPE I,
INDEX_SET TYPE STANDARD TABLE OF SY-TABIX WITH
NON-UNIQUE DEFAULT KEY INITIAL SIZE 10,
WA_INDEX_SET TYPE SY-TABIX.
LOOP AT NAMETAB TRANSPORTING NO FIELDS WHERE NAME CS 'Walter'.
WA_INDEX_SET = SY-TABIX.
APPEND WA_INDEX_SET TO INDEX_SET.
ADD 1 TO COUNT.
ENDLOOP.
... WHERE logexp
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.
LOOP AT ITAB INTO STRUC WHERE BLNCE <> 0.
WRITE: STRUC-NAME, STRUC-BLNCE.
ENDLOOP.
which has the same effect as:
LOOP AT ITAB INTO STRUC.
CHECK STRUC-BLNCE <> 0.
WRITE: / STRUC-NAME, STRUC-BLNCE.
ENDLOOP.
You can replace any operand - unless it is a subfield of the line structure of the relevant internal
table itab - with a functional method call. Function al
methods are methods in
ABAP Objects that can have any number of IMPORTING parameters and a RETURNING parameter.
Depending on the number of IMPORTING parameters, the following syntax options are available:
CLASS c1 DEFINITION.
PUBLIC SECTION.
METHODS M1 IMPORTING NAME TYPE
S_CARRNAME
RETURNING VALUE(CARR)
TYPE S_CARR_ID.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD M1.
SELECT
CARRID UP TO 1 ROWS
FROM SCARR
INTO
CARR
WHERE CARRNAME = NAME.
ENDSELECT.
ENDMETHOD.
ENDCLASS.
DATA: JTAB TYPE STANDARD TABLE OF SPFLI,
LINE
TYPE SPFLI.
DATA: OREF TYPE REF TO C1.
START-OF-SELECTION.
SELECT
* FROM SPFLI INTO TABLE JTAB.
CREATE OBJECT oref TYPE c1.
LOOP
AT JTAB INTO LINE
WHERE CARRID = OREF->
M1( 'Lufthansa' ).
WRITE: / LINE-CARRID, LINE-CONNID,
LINE-CITYFROM, LINE-CITYTO.
ENDLOOP.
The actual parameters are passed to the IMPORTING parameters
of the method as described in the CALL METHOD statement.
If the line type of the internal table is - or contains - a reference variable, the attributes of the object (to which the reference points in a given line) can be used as the first field in a comparison.
see Using Object Attributes as Internal Table Keys
You cannot address parts of a field in a table without a structured line type (for example, tables of
type
I). However, you can use the TABLE_LINE notation
to address the entire line within a WHERE condition (see
also The Pseudo-Component TABLE_LINE in Internal Tables).
The WHERE condition applied to a type I table
DATA: inttab TYPE STANDARD TABLE OF i WITH NON-UNIQUE
DEFAULT
KEY,
wa_inttab TYPE I.
LOOP AT inttab INTO wa_inttab WHERE table_line > 5 AND
table_line
<= 10.
...
ENDLOOP.
Non-Catchable Exceptions
Loop Structures:
DO, WHILE
Table Processing:
APPEND,
COLLECT, INSERT,
MODIFY, DELETE,
SORT, AT NEW/END OF/FIRST/LAST, READ TABLE.
Processing Lines of Tablesin Loops
Index Entries in Loops