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

Loop inside a loop is recommended or not?

Former Member
0 Likes
13,200

Hi guys,

I have a code as follows.

PARAMETERS: P_DC LIKE MARC-WERKS OBLIGATORY.

loop at it_x.

abc = p_dc. //p_dc is input parameter.

Some Logic/function modules are called.......

endloop.

Now i have to change p_dc as s_dc i.e. select option in input screen.

SELECT-OPTIONS: S_DC FOR MARC-WERKS OBLIGATORY.

So in above logic -- 1 way to do this is writting loop inside a loop to process that logic for every record in s_dc in above code.

IS IT RECOMMENED TO WRITE LOOP INSIDE A LOOP?

If not then what is another way to handle this?

As i am trying to find out some other way than loop since morning but not able to find the way out.

Kindly help.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
5,807

Hi Kiran Chorge ,

In General it's not recommended to use LOOP inside LOOP..

But at last it all depends on the requirement... there are cases where you have to write loop inside loop as per the user requirement...

If you want to avoid LOOP inside LOOP then you can use READ statement for the header table data selection in general...

It all depends on the requirement..

As you are using WERKS ==> Plant

It will not be having much impact because in master data there will be countable number of plants present...

Hope it will solve your problem..

Thanks & Regards

ilesh 24x7

ilesh Nandaniya

15 REPLIES 15
Read only

christine_evans
Active Contributor
0 Likes
5,807

Have you tried searching this site using loop within loop....?

Read only

Former Member
0 Likes
5,807

hi kiran..

definite NO for loop inside loop.

you can either read the select option and modify the it_x according to that first or fetch according to s_dc while writing select statements.

you can compare select option values as abc in s_dc etc.

Read only

Former Member
0 Likes
5,807

Hi Kiran,

First of all, i'm not pretty sure about your requirement.

If you can clearly explain you requirement, i can help you out.

Regarding loop inside a loop, Its usually not recommended because of Performance Issues. But there are cases, when you can't avoid using the same.

Regards,

Asha

Read only

Former Member
0 Likes
5,808

Hi Kiran Chorge ,

In General it's not recommended to use LOOP inside LOOP..

But at last it all depends on the requirement... there are cases where you have to write loop inside loop as per the user requirement...

If you want to avoid LOOP inside LOOP then you can use READ statement for the header table data selection in general...

It all depends on the requirement..

As you are using WERKS ==> Plant

It will not be having much impact because in master data there will be countable number of plants present...

Hope it will solve your problem..

Thanks & Regards

ilesh 24x7

ilesh Nandaniya

Read only

Former Member
0 Likes
5,807

Well if you want to avoid nested loop try this,



SELECT-OPTIONS: S_DC FOR MARC-WERKS OBLIGATORY.

data: count type i.

loop at it_x.
count = count + 1.
read table s_dc index count.
if sy-subrc = 0.
abc = s_dc-low.                        //s_dc is input parameter.
endif.
Some Logic/function modules are called.......
endloop.

Regards,

Vikranth

Read only

0 Likes
5,807

Hi Vikranth,

But in my case it_x for eg. has 10 records and s_dc hav say 3 records.

Now i have to execute that logic for every record of it_x with every record of s_dc..

its like

it_x-1 with s_dc-1

it_x-1 with s_dc-2

it_x-1 with s_dc-3

it_x-2 with s_dc-1

it_x-2 with s_dc-2

it_x-2 with s_dc-3

it_x-3 with s_dc-1

it_x-3 with s_dc-2

it_x-4 with s_dc-3

so on......

Read only

0 Likes
5,807

Hello Kiran,

That wouldnt be possible without using the nested loops. The parallel processing technique will be applicable only if its a M:1 relationship. In case of M:M relationship, nested loop is unavoidable.

Since you are just assigning values of the internal table with the select option values there shouldnt be much of a performance constraint even if you use nested loop here.

Regards,

Vikranth

Read only

Former Member
0 Likes
5,807

Hi ,

Dont use loop inside loop,

The better way is u can use loop at one internaltable1 to workarea1 .

then read internaltable2 with key belnr = workarea1-belnr.

This improves ur performance alot.

Read only

sreeramkumar_madisetty
Active Contributor
0 Likes
5,807

Hi

That's purely depends on your requirement.

If there is no way other than Loop Inside Loop,we can do that.

Regards,

Sreeram

Read only

Former Member
0 Likes
5,807

Hi Kiran,

Try this way..


data:
t_werks type table of marc-werks with header line.
select werks 
into table t_werks
from marc
where werks in s_dc.

//The selection table s_dc vl contain only 2 rec s_dc-low & s_dc-high.

the above code vl get u all the entries in b/w s_dc-low & s_dc-high.


data: w_idx type i value '1',
         w_lines type i.
describe table t_werks lines w_lines. // this gets no. of lines in t_werks.
loop at it_x.
read table t_werks index w_idx.
if sy-subrc eq 0.
abc = t_werks.
add 1 to w_idx.
endif.
if w_idx gt w_lines.
  w_idx = 1.
endif.
// othr functionality.......
endloop.

Hope it helps....

Regards,

Mdi.Deeba

Edited by: Mdi.Deeba on Sep 15, 2009 6:02 PM

Read only

0 Likes
5,807

Hi Mdi.Deeba

Ur first part is really helpfull.

But as per my understanding in second part...

u r assuming only 1 record of s_dc per it_x record..which is not the case

its like for every record read from it_x, i have to process say 3 records which r in s_dc

SO if it_x hase say 5 recs and s_dc has 2 recs..then for every rec in it_x i have to consider 2 recs of s_dc.

Thx for ur reply

Read only

0 Likes
5,807

Hi Kiran ,

Here is the modified code.....


data: w_idx type i value '1',
         w_lines type i.
describe table t_werks lines w_lines. // this gets no. of lines in t_werks.
loop at it_x.
*do w_lines times.*
read table t_werks index w_idx.
if sy-subrc eq 0.
abc = t_werks.
add 1 to w_idx.
endif.
if w_idx gt w_lines.
  w_idx = 1.
endif.
// othr functionality.......
*enddo*
endloop.

else u can also prefer loop inside loop bt thr are performance issues...


loop at it_x.
  loop at t_werks.
    abc = t_werks.
  // othr functionality.......
  endloop.
endloop.

Read only

Former Member
0 Likes
5,807

Hey Kiran

It is usually not recommended to use Loop within a Loop as the performance of the program worsens. But there are few scenarios where the Loop within Loop concept cannot be avoided and those cases we can use a Read Statement with a condition that satisfies your requirement. If that also cannot be used then go for Loop within Loop logic but make sure that you use the below algorithm so the performance of the program is not affected.

Algorithm for Nested Loops:

If ITAB1 has n1 entries and ITAB2 has n2 entries, the time needed for the nested loop with the straightforward algorithm is O(n1 * n2), whereas the parallel cursor approach takes only O(n1 + n2) time.

The below parallel cursor algorithm assumes that ITAB2 contains only entries also contained in ITAB1. If this assumption does not hold, the parallel cursor algorithm gets slightly more complicated, but its performance characteristics remain the same.

  • Entries: 100 (ITAB1), 1000 (ITAB2)

  • Line width: 100

  • Both tables sorted by key K

LOOP AT ITAB1 INTO WA1.

LOOP AT ITAB2 INTO WA2

WHERE K = WA1-K.

" ...

ENDLOOP.

ENDLOOP.

**********************************************

  • Entries: 100 (ITAB1), 1000 (ITAB2)

  • Line width: 100

  • Both tables sorted by key K

I = 1.

LOOP AT ITAB1 INTO WA1.

LOOP AT ITAB2 INTO WA2 FROM I.

IF WA2-K <> WA1-K.

I = SY-TABIX.

EXIT.

ENDIF.

Thank,

Harini

Read only

Former Member
0 Likes
5,807

LOOP INSIDE THE LOOP IS RECOMENDED, BECAUSE IT IS IMPLEMENTED FOR HIERARCHICAL TABLES. WHERE AS LOOP WITH READ IS PREFERED FOR PEER TABLES.

IT IS COMMONLY USED CONCEPT IN REAL TIME.

Read only

Former Member
0 Likes
5,807

hI GUYS ,

THX FOR UR REPLIES...

AT THE END I HAVE DECIDED TO USE LOOP STATEMENT..AS I THINK THERE IS NO OTHER WAY OUT.