LOOP statement in ABAP programming is used for looping through extracts, internal tables and screen fields. LOOP can have a nested structure. Means inside a LOOP block another LOOP statement can be used. Now lets us check the LOOP statement for each of these operations.
The control block with LOOP statement should end with ENDLOOP statement. One by one record of the extract dataset will be read in each loop pass. We can store the extracted fields in other field variables inside the loop statement for other processing.
Internal Tables Looping
LOOP AT <itab> INTO <wa> WHERE <logexp>.
LOOP AT <itab> ASSIGNING <FS> WHERE <logexp>.
LOOP AT <itab> REFERENCE INTO <dref> WHERE <logexp>.
LOOP AT <itab> TRANSPORTING NO FIELDS WHERE <logexp>.
- “LOOP AT” statement is used for looping through internal tables. The control block should end with ENDLOOP statement.
- WHERE addition is used for specifying a logical expression with the statement. The current record will process only if the logical expression is true for that record.
- INTO addition is used for storing the current loop record into a work area.
- ASSIGNING statement is used for assigning the current loop record to a field symbol.
- We can also use FROM, TO additions with index tables for restricting the lines to process.
The main run-time errors with internal table looping are
SCREEN fields looping
LOOP AT SCREEN [INTO wa].
“LOOP AT SCREEN” statement is used for looping through the fields on current screen. We can retrieve name and other attributes of each screen field in the loop iteration. The main components of screen field are listed below.
System field sy-subrc with Loop
With the LOOP statement system field sy-subrc will have the following values.
- sy-subrc=0 (Loop executed successfully at least once)
- sy-subrc=4 (Loop was not executed)
See a sample ABAP program with nested LOOP structure.
Also Read : View Other ABAP Keywords & Syntax -> ABAP Transaction codes