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: 
Read only

Dynamic programming using FOR Expressions

Former Member
0 Likes
1,836

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
Read only

retired_member
Product and Topic Expert
Product and Topic Expert
0 Likes
1,251

"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
Read only

retired_member
Product and Topic Expert
Product and Topic Expert
0 Likes
1,252

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

Read only

0 Likes
1,251

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

Read only

0 Likes
1,251

Edit: I enhanced the answer.

Read only

pokrakam
Active Contributor
0 Likes
1,251

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