The EXIT statement is a control statement in ABAP used to immediately and unconditionally terminate the execution of a specific processing block. The main use cases for EXIT are:
- Exiting loops: Prematurely leaving an entire
DO,WHILEorLOOP ATloop. - Exiting subroutines: Prematurely leaving a
FORM ... ENDFORMsubroutine.
Unlike CONTINUE (which only skips the current loop iteration) or CHECK (which conditionally skips or exits), EXIT terminates the entire enclosing block (loop or FORM) definitively.
Syntax
The basic and most commonly used syntax is:
EXIT.There are also specialized forms like EXIT FROM STEP-LOOP or EXIT FROM SQL, but these are less relevant in modern ABAP development.
Behavior
The behavior of EXIT depends on the context:
1. Inside a Loop (DO, WHILE, LOOP AT)
EXITimmediately terminates the entire loop, regardless of how many iterations were still planned.- Program execution continues with the first statement after the closing statement of the loop (
ENDDO,ENDWHILE,ENDLOOP). - With nested loops (a loop inside another),
EXITalways only terminates the innermost loop it is directly placed in.
2. Inside a Subroutine (FORM ... ENDFORM)
EXITimmediately terminates the entire subroutine.- Control returns to the calling program part, specifically to the statement directly after the
PERFORMcall.
3. In Other Contexts
- Using
EXITdirectly in event blocks (e.g.,START-OF-SELECTION) to end the entire program is technically possible but is considered bad programming style and obsolete.LEAVE PROGRAMshould be used instead. - In modern ABAP methods (
METHOD ... ENDMETHOD),EXITis not used to leave the method. TheRETURNstatement is intended for that purpose.
Distinction from Other Control Statements
It’s important to distinguish EXIT from similar statements:
CONTINUE: (Only in loops) Skips the rest of the current loop iteration and starts the next iteration. The loop itself is not terminated.CHECK <condition>.: (In loops/FORMs) Skips the rest of the iteration (CONTINUE) or exits the FORM (EXIT), but only if the condition is false.RETURN: (In methods/function modules) Terminates the execution of the method or function module and returns to the caller.LEAVE PROGRAM: Terminates the entire ABAP program.EXIT: Terminates the entire loop or the entire FORM unconditionally.
Examples
1. EXIT in LOOP AT (Abort Search)
DATA: product_list TYPE STANDARD TABLE OF string, search_item TYPE string VALUE 'Banana'.
APPEND 'Apple' TO product_list.APPEND 'Pear' TO product_list.APPEND 'Banana' TO product_list.APPEND 'Orange' TO product_list.
WRITE: / 'Searching for:', search_item.DATA found TYPE abap_bool VALUE abap_false.
LOOP AT product_list INTO DATA(product). WRITE: / 'Checking:', product. IF product = search_item. found = abap_true. WRITE: ' -> Found! Exiting loop.'. EXIT. " Exit the LOOP AT statement ENDIF.ENDLOOP.
IF found = abap_true. WRITE: / 'Item was found in the list.'.ELSE. WRITE: / 'Item was not found.'.ENDIF.WRITE: / 'Processing continues after the loop.'.Output:
Searching for: BananaChecking: AppleChecking: PearChecking: Banana -> Found! Exiting loop.Item was found in the list.Processing continues after the loop.2. EXIT in WHILE (Condition in Loop Body)
DATA counter TYPE i VALUE 0.DATA limit TYPE i VALUE 5.
WHILE counter < 10. " Theoretically up to 9 counter = counter + 1. WRITE: / 'WHILE loop, counter:', counter.
IF counter = limit. WRITE: ' -> Limit reached, exiting WHILE loop.'. EXIT. " Exit the WHILE loop ENDIF.
" This part is not reached after EXIT WRITE: ' -> Processing normally.'.ENDWHILE.
WRITE: / 'After WHILE loop.'.Output:
WHILE loop, counter: 1 -> Processing normally.WHILE loop, counter: 2 -> Processing normally.WHILE loop, counter: 3 -> Processing normally.WHILE loop, counter: 4 -> Processing normally.WHILE loop, counter: 5 -> Limit reached, exiting WHILE loop.After WHILE loop.3. EXIT in FORM (Leave Subroutine)
START-OF-SELECTION. PERFORM check_value USING 50. PERFORM check_value USING -10. " Error case PERFORM check_value USING 100.
FORM check_value USING iv_value TYPE i. WRITE: / 'FORM entered with value:', iv_value.
IF iv_value < 0. WRITE: ' -> Invalid value, exiting FORM.'. EXIT. " Exit the FORM subroutine ENDIF.
" This part is only reached for positive values WRITE: ' -> Value is valid, processing...'.ENDFORM.
WRITE: / 'Main program continues.'.Output:
FORM entered with value: 50 -> Value is valid, processing...FORM entered with value: -10 -> Invalid value, exiting FORM.FORM entered with value: 100 -> Value is valid, processing...Main program continues.4. EXIT in Nested Loops
DO 2 TIMES. " Outer loop DATA(outer_idx) = sy-index. WRITE: / 'Outer Loop:', outer_idx. DO 3 TIMES. " Inner loop DATA(inner_idx) = sy-index. WRITE: | Inner Loop: { inner_idx }|. IF outer_idx = 1 AND inner_idx = 2. WRITE: ' -> Exiting INNER loop!'. EXIT. " Exits the inner DO loop only ENDIF. ENDDO. " End of inner loop WRITE: | -> After Inner Loop (Outer: { outer_idx })|.ENDDO. " End of outer loopOutput:
Outer Loop: 1 Inner Loop: 1 Inner Loop: 2 -> Exiting INNER loop! -> After Inner Loop (Outer: 1)Outer Loop: 2 Inner Loop: 1 Inner Loop: 2 Inner Loop: 3 -> After Inner Loop (Outer: 2)Important Notes / Best Practice
EXITis unconditional. It immediately terminates the block when reached.- Therefore,
EXITis almost always placed inside anIFstatement to make the exit dependent on a specific condition (IF <condition> THEN EXIT. ENDIF.). - Use
EXITdeliberately when continuing loop or FORM processing no longer makes sense or an abort is required (e.g., after finding an element, on error). - Know the alternatives (
CONTINUE,CHECK,RETURN,LEAVE PROGRAM) and use them according to their purpose.