Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic programming using FOR Expressions

Former Member
0 Kudos
1,058

dynamic-programming.png

Hello Team,

I read somewhere all LOOP statement can be tweaked to form FOR statement. But I wasn't able to achieve it.

I am trying to achieve a dynamic looping using FOR statement. Is FOR supports such a requirement by somehow?

In my case, iteration is dependent on certain values. So can I have such a flexibility using FOR statement. These values are not possible to pass in WHERE conditions.

"<lt_change_log> is dynamic internal table
LOOP AT <lt_change_log> ASSIGNING FIELD-SYMBOL(<ls_change_log>).
ASSIGN COMPONENT 'CDHDR' OF STRUCTURE <ls_change_log> TO <ls_cdhdr>.
LOOP AT <ls_change_log>-cdpos_tab[] ASSIGNING FIELD-SYMBOL(<ls_cdpos>)WHERE changenr = <ls_cdhdr>-changenr.
 if <ls_cdpos>-new_value is not initial.
  ....
 else.
  continue.
 endif.
ENDLOOP.
ENDLOOP.

1 ACCEPTED SOLUTION

retired_member
Product and Topic Expert
Product and Topic Expert
0 Kudos
473

"Is FOR supports such a requirement by somehow?"

See the documentation,

https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/index.htm?file=abenfor_cond.htm

https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/index.htm?file=abenfor_cond.htm

Dynamic WHERE is possible and also nested FORs should work.

What you can't do, is to nest statements as ASSIGN COMPONENT. Only other expressions are possible. And for that you still need LOOP. But that's OK, since expressions don't make statements obsolete. They have another scope.

4 REPLIES 4

retired_member
Product and Topic Expert
Product and Topic Expert
0 Kudos
474

"Is FOR supports such a requirement by somehow?"

See the documentation,

https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/index.htm?file=abenfor_cond.htm

https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/index.htm?file=abenfor_cond.htm

Dynamic WHERE is possible and also nested FORs should work.

What you can't do, is to nest statements as ASSIGN COMPONENT. Only other expressions are possible. And for that you still need LOOP. But that's OK, since expressions don't make statements obsolete. They have another scope.

0 Kudos
473

Hi Horst,

OK thank you for the information. So for now I would understand that, FOR cannot support dynamic programming.. So LOOP ENDLOOP is still essential..

0 Kudos
473

Edit: I enhanced the answer.

pokrakam
Active Contributor
0 Kudos
473

Why? What's the point of replacing LOOP? Where did you read this?