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 within a loop

Former Member
0 Likes
3,209

Howdy again,

Just a quick question:

I've got the following loop:

<b>loop at bool_data-bseg into bseg.

check bseg-bukrs = '4001'.

loop at itab_setleaf_type.

check bkpf-blart LE itab_setleaf_type-valto.

endloop.

loop at itab_setleaf_vend.

check bseg-lifnr LE itab_setleaf_vend-valto.

endloop.

endloop.</b>

for the two loops within the main loop I weant the program to go onto the next record in the main loop.

Would using check do this - I don't think so...

Does anyone know a command that would?

Thanks!

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,671

As Jeet pointed out, EXIT inside a LOOP will terminate the LOOP and continue executing at the next statement following ENDLOOP.

For reference, I will call your loops - 1, 2 and 3.

So in loop 2, if you EXIT, then execution would continue with loop 3.

If you want the EXIT in loop 2 to skip loop 3 and continue loop 1, then you will need a flag.

For example:

LOOP 1
Flag = 'Y'.
  LOOP 2.
    IF ...
      Flag = 'N'.
      EXIT.
    ENDIF.
  ENDLOOP.
  IF Flag = 'N'.
    CONTINUE.
  ENDIF.
  LOOP 3.
    IF ...
      EXIT.
    ENDIF.
  ENDLOOP.
ENDLOOP.

3 REPLIES 3
Read only

Former Member
0 Likes
1,671

Hello,

The following code will take away lots of performance.

First of all you can use LOOP AT ITAB where <Condn>

Then the CHECK can be replaced by IF ... ENDIF.

Please derive the program logic as per the need. I hope the above points helps you.

Regards, Murugesh AS

Read only

Former Member
0 Likes
1,671

Steve,

I agree with Murugresh on the performance part.As far as coming out of inner loop use EXIT statement with if conditions. (Will come out only from inner loop.)

Jeet

Read only

Former Member
0 Likes
1,672

As Jeet pointed out, EXIT inside a LOOP will terminate the LOOP and continue executing at the next statement following ENDLOOP.

For reference, I will call your loops - 1, 2 and 3.

So in loop 2, if you EXIT, then execution would continue with loop 3.

If you want the EXIT in loop 2 to skip loop 3 and continue loop 1, then you will need a flag.

For example:

LOOP 1
Flag = 'Y'.
  LOOP 2.
    IF ...
      Flag = 'N'.
      EXIT.
    ENDIF.
  ENDLOOP.
  IF Flag = 'N'.
    CONTINUE.
  ENDIF.
  LOOP 3.
    IF ...
      EXIT.
    ENDIF.
  ENDLOOP.
ENDLOOP.