In this post, you will learn

  • For Loop for Internal Tables
  • Keyword LET
  • Nested For Loops

Visit ABAP 7.4+ Syntaxes / ABAP Expressions to read all the posts from the series ABAP 7.4 and beyond.

FOR Iteration for Single Table

FOR is also called Iteration Expression.

Consider below code

This is a simple For Loop for table lt_flights. Index keyword is optional here. This code simply transfers data from one table to another.

This is another way to write LOOP AT and APPEND kind of code.

Similar to LOOP AT, you can also write where conditions except the mandatory ( ).

FOR Iteration and LET expression

LET can be used to define variables and assign them to target table fields.

Nested FOR Iterations

This is similar to LOOP inside a LOOP. This way multiple FOR Iterations can be nested.

Code in text format

TYPES : BEGIN OF ty_flight,
          seq_num type i,
          carrier TYPE s_carrname,
          connect TYPE s_conn_id,
          fldate  TYPE s_date,
        END OF ty_flight.

DATA lt_new_flights TYPE STANDARD TABLE OF ty_flight.

SELECT * FROM sflight INTO TABLE @DATA(lt_flights).
IF sy-subrc EQ 0.
  SELECT * FROM scarr INTO TABLE @DATA(lt_scarr).

ENDIF.

"FOR Iteration 
lt_new_flights =
  VALUE #(
    FOR ls_flight IN lt_flights INDEX INTO lv_index
                                WHERE ( carrid = 'AA' AND
                                        connid = '0017' )
    LET lv_carrname = lt_scarr[ carrid = ls_flight-carrid ]-carrname
    IN  carrier = lv_carrname
    ( seq_num = lv_index
      connect = ls_flight-connid
      fldate  = ls_flight-fldate
    )
  ).

cl_demo_output=>display( lt_new_flights ).
"LOOP AT Method 
DATA: ls_new_flight TYPE ty_flight.
LOOP AT lt_flights INTO DATA(ls_flight).
  ls_new_flight-seq_num = sy-tabix.
  ls_new_flight-carrier = lt_scarr[ carrid = ls_flight-carrid ]-carrname.
  ls_new_flight-connect = ls_flight-connid.
  ls_new_flight-fldate  = ls_flight-fldate.
  APPEND ls_new_flight TO lt_new_flights.
ENDLOOP.

cl_demo_output=>display( lt_new_flights ).
"Nested FOR Iterations
lt_new_flights =
  VALUE #(
    FOR ls_scarr in lt_scarr
    FOR ls_flight IN lt_flights WHERE ( carrid = ls_scarr-carrid )
    (
      carrier = ls_scarr-carrname
      connect = ls_flight-connid
      fldate  = ls_flight-fldate
    )
  ).

Read about more such ABAP expressions and exciting new syntaxes: ABAP Expressions (7.4+)


If you like the content, please subscribe…

Join 4,013 other subscribers

Discovering ABAP YouTube Channel