What’s New In ABAP – Series 01, Episode 07 – Iteration Expression FOR

Iteration Expression:

To define iteration expression you can use the iteration operator ‘FOR’. Along with this there is another operator ‘REDUCE’ depends on the iteration expressions. When we use ‘FOR’ operator it works just like a loop at itab. It assigns a value to work area or field-symbols for each loop. This can be used with VALUE to populate data.

General Syntax:
FOR wa|<fs> IN itab [INDEX INTO idx] [cond]

The work area in the expression is local to itself. Here INDEX is nothing but the sy-tabix. Now we go through couple of examples on ‘FOR’ to understand better.

FOR Simple Expression: 

E.g: 1 To populate single column internal table.

Prior to ABAP 7.40:

LOOP AT lt_emp_info INTO ls_emp_info.
ls_address-name = ls_emp_info-name.
APPEND ls_address TO lt_addresses.
ENDLOOP.

With ABAP 7.40:

DATA: lt_addresses TYPE STANDARD TABLE OF char30

lt_addresses = VALUE #( FOR ls_emp_info IN lt_emp_info( ls_en_info-name ) ).

Explanation:

1. Here ls_emp_info is the work area of type lt_emp_info which is determined at the run time.

2. Now whatever the value you want to pass to the itab on the left side you need to specify as above ‘(ls_emp_info-name)’ and this value is populated to the table lt_addresses.

 

next episode LET me Express (coming next week) or series home page

FOR with WHERE Condition:

E.g: 2

Prior to ABAP 7.40:

LOOP AT lt_emp_info INTO ls_emp_info WHERE name = 'IYC'.
ls_address-name = ls_emp_info-name.
APPEND ls_address TO lt_addresses.
ENDLOOP.

With ABAP 7.40:

DATA: lt_addresses TYPE STANDARD TABLE OF char30.
lt_addresses = VALUE #( FOR ls_emp_info IN lt_emp_info WHERE ( name = 'IYC' ) ( ls_em_info-name ) ).

FOR with THEN, UNTIL & WHILE:

Until – Iterates through the internal table until the condition fails.

While – iterates through as long as the condition is true.

Then – it comes with expression, we can specify any expression.

E.g. 3:

TYPES:
BEGIN OF ty_line,
col1 TYPE i,
col2 TYPE i,
col3 TYPE i,
END OF ty_line,
ty_tab TYPE STANDARD TABLE OF ty_line WITH EMPTY KEY.

Prior to 7.40.

DATA: gt_itab TYPE ty_tab,
j       TYPE i.
FIELD-SYMBOLS <ls_tab> TYPE ty_line.

j = 1.
DO.
j = j + 10.
IF j > 40. EXIT. ENDIF.
APPEND INITIAL LINE TO gt_itab ASSIGNING <ls_tab>.
<ls_tab>–col1 = j.
<ls_tab>–col2 = j + 1.
<ls_tab>–col3 = j + 2.
ENDDO.

With ABAP 7.40

DATA(gt_itab) = VALUE ty_tab( FOR j = 11 THEN j + 10 UNTIL j > 40
( col1 = j col2 = j + 1 col3 = j + 2  ) ).

FOR in Nested case:

It is just like loop inside loop in our internal table processing.

E.g. 4:

If there is a situation where you need to populate an internal table by processing other two internal tables following example works fine.

Prior to ABAP 7.40

LOOP AT itab1 INTO wa1.
LOOP AT itab2 INTO wa2 WHERE field 1 = wa1-field 1.
*Populate your required internal table.
ENDLOOP.
ENDLOOP.

With ABAP 7.40.

DATA(lt_final_tab) = VALUE final_tab_type( FOR wa1 IN itab1
FOR wa2 IN itab2 WHERE ( subject = wa1-field_value ) ( wa2 - final_value ) ).

Explanation:

1. In the above example I have populated ‘lt_final_tab’ with the value of column name, ‘subject’ from itab2 based on the condition.

2. The Condition is dependent on the itab1. So itab1 is processed first. If you see the last line, after the ‘WHERE’ we have condition and immediately we are passing the value of ‘wa2-final_value’.

3. Which will be populated to the target internal table on the left side.

Similar way we can iterate through multiple table at a time using this FOR and we can make our code to look fancy and with better readability.

End of this section, go to next episode LET me Express (coming next week) or series home page

More Episodes:

Constructor Operators

Squeezing ABAP Code

Table Expressions

Internal table functions

Strings on me!