Friday 22 December 2017

How to Work With Check, Countinue, Exit, Stop and Return in SAP ABAP Program

1. Check Statement:
   Check Statement in ABAP is used to terminates a loop or Processing block based on a condition.
If the condition in the CHECK statement is false then all the remaining statements in the statement block after the CHECK statement are ignored, and the next loop pass starts.

Example1 :

DATA : VAR1 TYPE I VALUE 0.

WHILE  VAR1 < 5.

  VAR1 = VAR1 + 1.

  CHECK  VAR1 NE 2.

  WRITE / VAR1.

ENDWHILE.

output :
         1
         3
         4
         5

here is another example of check statement with subroutines.

Example2:
DATA : VAR1 TYPE I,
       VAR2 TYPE I,
       RESULT TYPE P DECIMALS 2.

VAR1 = 20. VAR2 = 4.
PERFORM DIVISION_RESULT USING VAR1 VAR2 CHANGING RESULT.
*&---------------------------------------------------------------------*
*&      Form  DIVISION_RESULT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_VAR1    text
*      -->P_VAR2    text
*      <--P_RESULT  text
*----------------------------------------------------------------------*
FORM DIVISION_RESULT  USING  P_VAR1
                             P_VAR2
                             CHANGING P_RESULT.
  CHECK P_VAR2 NE 0.

  P_RESULT = P_VAR1 / P_VAR2.
  WRITE :/ 'RESULT = ', P_RESULT.
ENDFORM.                    " DIVISION_RESULT

output : RESULT=      5.00


suppose if i assign 0 to var2 then the the output is nothing Because check statemnt check condition and ignored the FORM-ENDFORM Block,
and WIthout check Statement it will give the runtime errror.


2. Countinue Statement : 
CONTINUE statement is used to skip execution of a record inside loop-endloop, do-endo, while-endwhile etc.
CONTINUE terminates the current loop pass, returns the processing to the beginning of the loop and starts the next loop pass,
the execution of the remaining statements in the current processing loop is stopped and the next loop pass is processed.
This keyword will only be used in loops.

Example :
DATA : VAR1 TYPEVALUE 0.

WHILE  VAR1 < 5.

  VAR1 = VAR1 + 1.

  IF VAR1 = 2.
   CONTINUE.
  ENDIF.

  WRITE / VAR1.

ENDWHILE.

output :
         1
         3
         4
         5

3. Exit Statement :
EXIT statement is used to terminate an entire loop or Processing block based on a condition.
The behavior of Exit Statement is depends on where you use it.
If you use Exi  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 Subroutines.

Example:
DATA : VAR1 TYPE I VALUE 0.

WHILE  VAR1 < 5.

  VAR1 = VAR1 + 1.

  IF VAR1 = 2.
   EXIT.
  ENDIF.

  WRITE / VAR1.

ENDWHILE.

OUTPUT : 1


4. Stop Statement :
The STOP statement is only to be used in executable program and in the following event blocks:
 AT SELECTION-SCREEN
 START-OF-SELECTION
 GET
If I have two events START-OF-SELECTION and END-OF-SELECTION in executable program, If I use STOP Statement in START-OF-SELECTION, the Statement exit start-of-selection and goes to END-OF-SELECTION.

Example1 :
DATA: IT_MARA TYPE TABLE OF MARA,
      WA_MARA TYPE MARA.

START-OF-SELECTION.
  SELECT MATNR
         MTART
         MATKL FROM MARA INTO CORRESPONDING FIELDS OF 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.

OUTPUT : here output is nothing Because before Write Statement the Stop statement exit the start-of-selection event Block.

Example2 :
DATA: IT_MARA TYPE TABLE OF MARA,
      WA_MARA TYPE MARA.

START-OF-SELECTION.
  SELECT MATNR
         MTART
         MATKL FROM MARA INTO CORRESPONDING FIELDS OF 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.

OUTPUT : here output is Show according to Your Mara Table Because Stop Statement Exit the start-of-selection event Block After Fatching the data, and goes to END-OF-SELECTION Event block Where We USe
Write Statement.

5. Return Statement :
The Return statement ends the current processing block immediately.
The RETURN statement is provided for exiting processing blocks early but correctly.
However, since RETURN behaves differently in GET events than when the event block is exited as usual, you should instead use the REJECT statement there, which has been designed especially for this purpose.

Example1:
DATA: IT_MARA TYPE TABLE OF MARA,
      WA_MARA TYPE MARA.

START-OF-SELECTION.
  SELECT MATNR
         MTART
         MATKL FROM MARA INTO CORRESPONDING FIELDS OF TABLE IT_MARA UP TO 50 ROWS.
  RETURN.

END-OF-SELECTION.
  LOOP AT IT_MARA INTO WA_MARA.
    WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MATKL.
  ENDLOOP.
OUTPUT : here output is nothing.

Example2:
DATA: IT_MARA TYPE TABLE OF MARA,
      WA_MARA TYPE MARA.

START-OF-SELECTION.
  SELECT MATNR
         MTART
         MATKL FROM MARA  INTO CORRESPONDING FIELDS OF TABLE IT_MARA UP TO 50 ROWS.

END-OF-SELECTION.
  LOOP AT IT_MARA INTO WA_MARA.
    IF WA_MARA-MTART = 'FERT'.
    RETURN.
    ENDIF.
    WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MATKL.
  ENDLOOP.
OUTPUT : here output is Show according to Your Mara Table and The Return statement ends the current processing block immediately when Condition is True.

No comments:

Post a Comment

In this post we use some basics Events  and Control Break Statements for Generating Classical Report Based On single Table. If You want so...