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

To exit from a loop

Former Member
0 Likes
2,155

Dear all,

The structure of my code is as follows :-

LOOP AT ITAB. (1)

.....

....

DO. (2)

....

....

DO. (3)

....

....

IF I > 2.

....GOT TO NEXT LOOP PASS (1).

ENDIF.

ENDDO.

ENDDO.

ENDLOOP.

Which statement can I use to get out of the two DO loops and go to the next Loop pass of LOOP (1).

Please suggest.

Thanks in advance,

Archana

1 ACCEPTED SOLUTION
Read only

christian_wohlfahrt
Active Contributor
0 Likes
2,071

Hi Archana!

Easiest way: make a flag for this:


data next_loop type xflag.
loop at itab.
  clear next_loop.
  do.
    do.
      if ...
        next_loop = 'X'.
        exit. (3)
      endif.
    enddo.
    if next_loop = 'X'.
      exit.   (2)
    endif.
  enddo.
  if next_loop = 'X'.
    continue. (1)
  endif.
  ... (optional coding)
endloop.

Regards,

Christian

Hi Archana!

Easiest way: make a flag for this:


data next_loop type xflag.
loop at itab.
  clear next_loop.
  do.
    do.
      if ...
        next_loop = 'X'.
        exit. (3)
      endif.
    enddo.
    if next_loop = 'X'.
      exit.   (2)
    endif.
  enddo.
  if next_loop = 'X'.
    continue. (1)
  endif.
  ... (optional coding)
endloop.

Regards,

Christian

7 REPLIES 7
Read only

christian_wohlfahrt
Active Contributor
0 Likes
2,072

Hi Archana!

Easiest way: make a flag for this:


data next_loop type xflag.
loop at itab.
  clear next_loop.
  do.
    do.
      if ...
        next_loop = 'X'.
        exit. (3)
      endif.
    enddo.
    if next_loop = 'X'.
      exit.   (2)
    endif.
  enddo.
  if next_loop = 'X'.
    continue. (1)
  endif.
  ... (optional coding)
endloop.

Regards,

Christian

Read only

0 Likes
2,071

Hi Christian,

Thanx so much....

Ur solution has solved my problem...

have rewarded u the points as well...

Read only

Former Member
0 Likes
2,071

Hi Archana,

There is not one statement to get out of the innermost DO-loop and the outermost DO-loop at once.

You will have to program 2 consecutive EXITs (one for each DO-ENDDO) to accomplish this.

Regards,

John.

Read only

Former Member
0 Likes
2,071

Hi Archana,

You can use CONTINUE to come out of the current loop.

see below example i hope it may help you.

DO 100 TIMES.

IF SY-INDEX >= 10 AND SY-INDEX <= 20.

CONTINUE.

ENDIF.

...

ENDDO.

if solution is helpful kindly reward the points

Read only

Former Member
0 Likes
2,071

Hi,

U can use EXIT or STOP Statement.

Simple example of a DO loop:

DO.

WRITE SY-INDEX.

IF SY-INDEX = 3.

EXIT.

ENDIF.

ENDDO.

The output is:

1 2 3

Check this link,

<u>http://help.sap.com/saphelp_erp2004/helpdata/en/fc/eb3564358411d1829f0000e829fbfe/frameset.htm</u>

Thanks&Regards,

Ruthra

Read only

Former Member
0 Likes
2,071

There is no single staemant you can use - you can EXIT to extr to DO-LOOP. But to go to the next LOOP you have to use CONTINUE.

Define a help-variable ex. l_exit and set this to TRUE if you want to exit and check for this.

Read only

Former Member
0 Likes
2,071

Hi!

The idea is the following - place DO(2) and DO(3) loops inside separate FORMS and handle return parameters from them bottom-up.

Regards,

Maxim.