You are on page 1of 3

SAPNuts.

com
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 returnstatements? 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).
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-OFSELECTION 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.

SAPNuts.com
Program1
REPORT ZSAPN_STOP. DATA: IT_MARA TYPE TABLE OF MARA, WA_MARA TYPE MARA. START-OF-SELECTION. SELECT * FROM MARA INTO TABLE IT_MAR A UP TO 50 ROWS. STOP. LOOP AT IT_MARA INTO WA_MARA. WRITE:/ WA_MARA-MATNR, WA_MARAMTART, WA_MARA-MATKL. ENDLOOP. Result: NO OUTPUT

Program2
REPORT ZSAPN_STOP. DATA: IT_MARA TYPE TABLE OF MARA, WA_MARA TYPE MARA. START-OF-SELECTION. SELECT * FROM MARA INTO TABLE IT_MAR A UP TO 50 ROWS. STOP. END-OF-SELECTION. LOOP AT IT_MARA INTO WA_MARA. WRITE:/ WA_MARA-MATNR, WA_MARAMTART, WA_MARA-MATKL. ENDLOOP. 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'. "Dont 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.

SAPNuts.com
Program1
REPORT ZSAPN_RETURN. DATA: IT_MARA TYPE TABLE OF MARA, WA_MARA TYPE MARA. START-OF-SELECTION. SELECT * FROM MARA INTO TABLE IT_MAR A 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_MARAMTART, WA_MARA-MATKL. ENDLOOP.

Program2
REPORT ZSAPN_RETURN. DATA: IT_MARA TYPE TABLE OF MARA, WA_MARA TYPE MARA. START-OF-SELECTION. SELECT * FROM MARA INTO TABLE IT_MAR A 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_MARAMTART, WA_MARA-MATKL. ENDLOOP.

RESULT: NO OUTPUT

RESULT: SOME RECORDS as program execution stops after HALB material type.

Original Tutorial is at: http://www.sapnuts.com/tutorials/