Skip to Content
author's profile photo Former Member
Former Member

To exit from a loop

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

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

6 Answers

  • Best Answer
    Posted on Oct 13, 2005 at 07:02 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 13, 2005 at 07:03 AM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 13, 2005 at 07:03 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 13, 2005 at 07:03 AM

    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,

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

    Thanks&Regards,

    Ruthra

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 13, 2005 at 07:03 AM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 13, 2005 at 07:04 AM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.