Working with EXIT, STOP, CONTINUE and RETURN in SAP ABAP详解编程语言

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'. "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.

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/20136.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论