ABAP LOOP Statement

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.

Extracts looping

Syntax:

LOOP.

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

Syntax Variations

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

  • ITAB_ILLEGAL_REG
  • MOVE_TO_LOOP_REF

 

SCREEN fields looping

Syntax

LOOP AT SCREEN [INTO wa].

ENDLOOP.

“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.

  1. name
  2. group1
  3. group2
  4. group3
  5. group4
  6. required
  7. input
  8. output
  9. intensified
  10. invisible
  11. length
  12. active
  13. display_3d
  14. value_help
  15. request
  16. values_in_combo

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