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

Exiting a loop

Former Member
0 Likes
883

This is my code

-


loop at itab. ---> loop 1

read table itab2 with key id = itab-id.

if sy-subrc = 4.

exit.

endloop.

Loop at itab3 -


>loop 2

endloop

-


Now I want that when whenever sy-subrc = 4 in loop 1... the exit should exit the whole block ie loop 2 also...But with my above code it is exiting only the first loop but passing thorught loop 2.

How should I solve my purpose...( Using continue is not helping either)

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
833
data : l_flag type c.

loop at itab. ---> loop 1
read table itab2 with key id = itab-id.
if sy-subrc = 4.
l_flag = 'X'.
exit.
endloop.

if l_flag is initial.
Loop at itab3 ---->loop 2
{ exp

}
endloop.
endif.
clear l_flag.

Best regards,

Prashant

5 REPLIES 5
Read only

Former Member
0 Likes
834
data : l_flag type c.

loop at itab. ---> loop 1
read table itab2 with key id = itab-id.
if sy-subrc = 4.
l_flag = 'X'.
exit.
endloop.

if l_flag is initial.
Loop at itab3 ---->loop 2
{ exp

}
endloop.
endif.
clear l_flag.

Best regards,

Prashant

Read only

Former Member
0 Likes
833

Hi Priya,

Try this way.

data: flag type c.

loop at itab. ---> loop 1

read table itab2 with key id = itab-id.

if sy-subrc = 4.

flag = 'X'.

exit.

endloop.

Loop at itab3 -


>loop 2

{ IF flag = 'X'.

exit.

ENDIF.

}

endloop

c

Read only

Former Member
0 Likes
833

Hi,

then, you can check the subrc while doing the second loop.

eg:

loop at itab. ---> loop 1

read table itab2 with key id = itab-id.

if sy-subrc = 4.

exit.

endloop.

IF sy-subrc EQ 0.

Loop at itab3 -


>loop 2

endloop

endif.

Hope it helps!!

Regards,

Pavan

Read only

0 Likes
833

>

> loop at itab. ---> loop 1

> read table itab2 with key id = itab-id.

> if sy-subrc = 4.

> exit.

> endloop.

>

> IF sy-subrc EQ 0.

> Loop at itab3 -


>loop 2

> endloop

> endif.

Pavan - SY-SUBRC will depend on whether or not there are entries in itab, not the READ TABLE itab2 statement.

Rob

Read only

Former Member
0 Likes
833

Hi

If ITAB3 is having relationship with either ITAB1 or ITAB2 then

loop at itab. ---> loop 1

read table itab2 with key id = itab-id.

if sy-subrc = 4.

exit.

else.

read table itab3 with key id = itab-id. or read table itab3 with key id = itab2-id.

if sy-subrc = 0.

*Exp

endif.

endif.

endloop.

The above code will improve the performance.

Regards

Shiva