Tutorial Name

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

Tutorial Description

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

Tutorial Area Core ABAP
Prerequisites ABAP
Learning Level Intermediate
Estimated Time to learn 30
Save to Learning List Save to your Learning List
+ -

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'. "Don’t print 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.

Author | Last Updated| | 22 Comments | F Share

Tutorial Comments

Total Comments: Add your Comment
06 Mar 2014

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.

10 Apr 2014

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

07 May 2014

Its was good information thanks for the tuts.

18 May 2014

helpful ,thank you...for sharing:)

25 May 2014

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

25 May 2014

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

25 May 2014

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

25 May 2014

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

01 Jun 2015

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

01 Jun 2015

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

01 Jun 2015

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

01 Jun 2015

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

01 Jun 2015

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

13 Jul 2015

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

12 Oct 2015

With example explanation is very useful.

23 Nov 2015

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

20 Jan 2016

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

09 Feb 2016

Thnaks for sharing its very nice information

01 Apr 2016

Great Explanation, Core Basics

01 Apr 2016

Great Explanation, Core Basics

22 Jun 2016

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

03 Nov 2016

great explanation. This is very helpful core basics.

Leave a Comment

Enter Comment Text