Working with EXIT, STOP, CONTINUE and RETURN in SAP ABAP

Author | Last Updated| | 38 Comments Facebook Share Twitter Share Google Plus Share A+ A-


Learn how to use STOP, EXIT, CONTINUE and RETURN commands in SAP ABAP, difference between STOP, EXIT, CONTINUE and RETURN in SAP ABAP

In our day to day ABAP programming implementations, we may need to use exit, continue, stop and return statements, this article will help you to understand what are exit, stop, continue and return statements? And when do we use them.

EXIT

The behavior of EXIT keyword is depends on where you use it.
  • If you use EXIT keyword inside IF .. ENDIF., it will comes out of the program.
  • If you use EXIT inside LOOP .. ENDLOOP., it will come out of loop.
  • If you use EXIT inside FORM .. ENDFORM., it will comes out of form (subroutine).
Example program os using EXIT keyword
REPORT ZSAPN_EXIT.
DATA: IT_MARA TYPE TABLE OF MARA,
      WA_MARA TYPE MARA.
PARAMETERS: P_MATNR TYPE MARA-MATNR.

START-OF-SELECTION.
  SELECT SINGLE * FROM MARA INTO WA_MARA WHERE MATNR = P_MATNR.

  IF WA_MARA IS INITIAL.
    EXIT. "exit program
  ENDIF.
  WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MATKL.

STOP

STOP is a statement which is used to stop processing an event block, ex: If I have two events START-OF-SELECTION and END-OF-SELECTION in my program, If I use STOP keyword in START-OF-SELECTION, the keyword will exits start-of-selection and goes to END-OF-SELECTION.

See the difference between below two programs.

Program1 Program2
REPORT ZSAPN_STOP.
DATA: IT_MARA TYPE TABLE OF MARA,
      WA_MARA TYPE MARA.

START-OF-SELECTION.
  SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS.
  STOP.

  LOOP AT IT_MARA INTO WA_MARA.
    WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MATKL.
  ENDLOOP.
REPORT ZSAPN_STOP.
DATA: IT_MARA TYPE TABLE OF MARA,
      WA_MARA TYPE MARA.

START-OF-SELECTION.
  SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS.
  STOP.

END-OF-SELECTION.
  LOOP AT IT_MARA INTO WA_MARA.
    WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MATKL.
  ENDLOOP.
Result: NO OUTPUT RESULT: OUTPUT WILL BE DISPLAYED

CONTINUE

CONTINUE is a statement, which is used to skip execution of a record inside loop.. endloop, do..endo, while..endwhile etc. This keyword will only be used in loops.

REPORT ZSAPN_CONTINUE.
DATA: IT_MARA TYPE TABLE OF MARA,
      WA_MARA TYPE MARA.
START-OF-SELECTION.
  SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS.

  LOOP AT IT_MARA INTO WA_MARA.
    IF WA_MARA-MTART = 'HALB'. "Skip  if material type is 'HALB'
      CONTINUE. "Skip the record and go for next record
    ENDIF.
    WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MATKL.
  ENDLOOP.
In the above program, the output will not display materials of type "HALB" because loop skips the records with HALB material type.

RETURN

RETURN is a statement which is used to stop processing of current block immediately.

Execute and see the difference between the below two programs.

Program1 Program2
REPORT ZSAPN_RETURN.
DATA: IT_MARA TYPE TABLE OF MARA,
      WA_MARA TYPE MARA.

START-OF-SELECTION.
  SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS.
  RETURN.

END-OF-SELECTION.
  LOOP AT IT_MARA INTO WA_MARA.
    IF WA_MARA-MTART = 'HALB'.

    ENDIF.
    WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MATKL.
  ENDLOOP.
REPORT ZSAPN_RETURN.
DATA: IT_MARA TYPE TABLE OF MARA,
      WA_MARA TYPE MARA.

START-OF-SELECTION.
  SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS.

END-OF-SELECTION.
  LOOP AT IT_MARA INTO WA_MARA.
    IF WA_MARA-MTART = 'HALB'.
RETURN.
    ENDIF.
    WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MATKL.
  ENDLOOP.
Result: NO OUTPUT RESULT: SOME RECORDS as program execution stops after "HALB" material type.

Tutorial Comments

Total Comments: Add your Comment
29 Mar 2018

A small caveat: according to SAP, the keyword EXIT is obsolete inside loops (as of v6.10), While we developers will certainly see EXIT inside loops in older code, it is not best practice to use EXIT inside loops for new development work and should therefore be called out in code reviews.

29 Mar 2018

good work......................

29 Mar 2018

Its was good information thanks for the tuts.

29 Mar 2018

helpful ,thank you...for sharing:)

29 Mar 2018

thank you...for sharing:) thanks................

29 Mar 2018

thank you...for sharing:) thanks................

29 Mar 2018

thank you...for sharing:) thanks................

29 Mar 2018

thank you...for sharing:) thanks................

29 Mar 2018

Thank u for sharing. and your tutorial was very useful for me.

29 Mar 2018

Thank u for sharing. and your tutorial was very useful for me.

29 Mar 2018

Thank u for sharing. and your tutorial was very useful for me.

29 Mar 2018

Thank u for sharing. and your tutorial was very useful for me.

29 Mar 2018

Thank u for sharing. and your tutorial was very useful for me.

29 Mar 2018

One of the best site i came cross . You explain each and every topic with simplicity which is the best thing .

29 Mar 2018

With example explanation is very useful.

29 Mar 2018

Gr8 Explanation and very use full . we expecting more topic other than given

29 Mar 2018

Thanks for sharing this use full information... ABAP online Training

29 Mar 2018

Thnaks for sharing its very nice information

29 Mar 2018

Great Explanation, Core Basics

29 Mar 2018

Great Explanation, Core Basics

29 Mar 2018

Well this was very nice article and very useful to SAP ABAP Tutorial learners.

29 Mar 2018

great explanation. This is very helpful core basics.

29 Mar 2018

ooh very good article shown in this page. sap basis online training sap bi online training

29 Mar 2018

It was helpful. Good way of explanation.

29 Mar 2018

tank you.......................

29 Mar 2018

tank you.......................

29 Mar 2018

Wow very good thank you for sharing....

29 Mar 2018

Nice Article very helpful, Thank you

29 Mar 2018

tank you.......................

29 Mar 2018

tank you.......................

29 Mar 2018

tank you.......................

29 Mar 2018

tank you.......................

29 Mar 2018

tank you.......................

29 Mar 2018

Very Useful...Tq.............................

29 Mar 2018

Nicely Explained ..... good work..!

29 Mar 2018

here both exit and continue are doing same work.then what is difference between them.

09 Apr 2018

The difference between exit, stop, return is unclear... they all seem to leave the processing block what's the difference?? Am I the only one wondering?

09 Apr 2018

@BRYANS Please find explanation below.

STOP: Will come out of the event block ex: START-OF-SELECTION, END-OF-SELECTION etc, even if you use STOP inside a sub-routine, it will come out of the event, if there is no further event, it will come out of the program.

SAP ABAP is an event driven programming language, will execute event by event only
  
START-OF-SELECTION.
  STOP.
  WRITE:/ 'Start of selection'.
END-OF-SELECTION.
  WRITE:/ 'End of selection'.
**Output: 'End of selection' 
START-OF-SELECTION.
PERFORM test.
  WRITE:/ 'Start of selection'.
END-OF-SELECTION.
  WRITE:/ 'End of selection'.
FORM test .
    STOP.
ENDFORM.
**Output: 'End of selection' 

RETURN: this will exit the current coding block(including event block), ex: perform, function module, event

START-OF-SELECTION.
PERFORM test.
  WRITE:/ 'Start of selection'.
END-OF-SELECTION.
  WRITE:/ 'End of selection'.
FORM test .
    RETURN.
    WRITE:/ 'Inside perform'.
ENDFORM.
** Start of selection
** End of selection
START-OF-SELECTION.
RETURN.
  WRITE:/ 'Start of selection'.
END-OF-SELECTION.
  WRITE:/ 'End of selection'.
**No output

EXIT: this will exit the current coding block, if you use directly, if you use thsi inside looping block ex: loop, it will come out of the looping block not coding block .

DATA: it_mara TYPE TABLE OF mara,
      wa_mara TYPE mara.
START-OF-SELECTION.
  PERFORM test.
  WRITE:/ 'Start of selection'.
END-OF-SELECTION.
  WRITE:/ 'End of selection'.
FORM test .
  SELECT *
  FROM mara INTO TABLE it_mara
  UP TO 10 ROWS.
  LOOP AT it_mara INTO wa_mara.
    IF sy-tabix EQ 2.
        EXIT.
    ENDIF.
    WRITE:/ wa_mara-matnr.
  ENDLOOP.
  WRITE:/ 'After loop'.
ENDFORM.

Leave a Comment

Enter Comment Text