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

Nested Counter Loop in ABAP

Former Member
0 Likes
1,470

Hey everyone,

is there a simpler/better way to make a nested counter loop than this?

  DATA(XCOUNT) = 0.
  DATA(YCOUNT) = 0.
  DO X TIMES.
    XCOUNT = XCOUNT + 1.
    DO Y TIMES.
      YCOUNT = YCOUNT + 1.
      DO=>SOMETHING( X = XCOUNT Y = YCOUNT ).
    ENDDO.
    YCOUNT = 0.
  ENDDO.
  XCOUNT = 0.
1 ACCEPTED SOLUTION
Read only

Sandra_Rossi
Active Contributor
0 Likes
1,307

Your code is so simple to understand that I don't see why you want to improve it. The only weird thing is common to many developers, is the "reset after execution" ("?count = 0"), as it's prone to errors and can be generally avoided. Instead, you should always prefer the "reset before execution" (move DATA(YCOUNT) = 0 before DO Y TIMES, and remove the 2 lines ?count = 0).

But if it's for the fun/NOT a productive code, you may declare DO=>SOMETHING with a dummy returning parameter and do like this (I use ASSERT here, but I could use anything else, it's just to say that the result is always 1 and has no importance for the objective):

ASSERT 1 = REDUCE i( INIT i = 1
    FOR xcount = 1 THEN xcount + 1 WHILE xcount <= x
    FOR ycount = 1 THEN ycount + 1 WHILE ycount <= y
    LET dummyresult = DO=>SOMETHING( X = XCOUNT Y = YCOUNT ) IN
    NEXT i = 1 ).

WARNING: stunt[wo]men, don't try this at home and at work!

3 REPLIES 3
Read only

MateuszAdamus
Active Contributor
1,307

Hello Former Member

From SAP Help (https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abapdo.htm): SY-INDEX always refers to the current LOOP

Which means you could do something like:

  DATA(XCOUNT) = 0.
  DO X TIMES.
    XCOUNT = XCOUNT + 1.
    DO Y TIMES.
      DO=>SOMETHING( X = XCOUNT Y = sy-index ).
    ENDDO.
  ENDDO.
  XCOUNT = 0.

You could also store X count in the class:

DO x TIMES.
  do=>set_x( sy-index ).
  DO y TIMES.
    do=>something( y = sy-index ).
  ENDDO.
ENDDO.

METHOD something.
  WRITE: / do=>x.
  WRITE: / y.
ENDMETHOD.

Kind regards,

Mateusz

Read only

matt
Active Contributor
0 Likes
1,307

Not fundamentally, but xcount and ycount are clearly helper variables and won't have use outside of the code you've posted, so something like:

DATA(XCOUNT) = 0.
DO XCOUNT TIMES.
  DATA(YCOUNT) = 0.
  XCOUNT = XCOUNT +1.
  DO YCOUNT TIMES.
      YCOUNT = YCOUNT + 1.
      DO=>SOMETHING( X = XCOUNT Y = YCOUNT ).
  ENDDO.
ENDDO.

or

DATA(XCOUNT) = 0.
DO XCOUNT TIMES.
  XCOUNT = XCOUNT + 1.
  yloop( XCOUNT ).
ENDDO.

METHOD yloop.
  DATA(YCOUNT) = 0.
  DO YCOUNT TIMES.
      YCOUNT = YCOUNT + 1.
      DO=>SOMETHING( I_X = I_X I_Y = YCOUNT ).
  ENDDO.
ENDMETHOD.
Read only

Sandra_Rossi
Active Contributor
0 Likes
1,308

Your code is so simple to understand that I don't see why you want to improve it. The only weird thing is common to many developers, is the "reset after execution" ("?count = 0"), as it's prone to errors and can be generally avoided. Instead, you should always prefer the "reset before execution" (move DATA(YCOUNT) = 0 before DO Y TIMES, and remove the 2 lines ?count = 0).

But if it's for the fun/NOT a productive code, you may declare DO=>SOMETHING with a dummy returning parameter and do like this (I use ASSERT here, but I could use anything else, it's just to say that the result is always 1 and has no importance for the objective):

ASSERT 1 = REDUCE i( INIT i = 1
    FOR xcount = 1 THEN xcount + 1 WHILE xcount <= x
    FOR ycount = 1 THEN ycount + 1 WHILE ycount <= y
    LET dummyresult = DO=>SOMETHING( X = XCOUNT Y = YCOUNT ) IN
    NEXT i = 1 ).

WARNING: stunt[wo]men, don't try this at home and at work!