‎2006 Nov 16 12:01 PM
‎2006 Nov 16 12:03 PM
Terminating Loops
ABAP contains termination statements that allow you to terminate a loop prematurely. There are
two categories of termination statement - those that only apply to the loop, and those that apply
to the entire processing block in which the loop occurs. The STOP and REJECT statements
belong to the latter group, and are described in more detail under Leaving Event Blocks [Page
966].
The termination statements that apply only to the loop in which they occur are CONTINUE,
CHECK, and EXIT. You can only use the CONTINUE statement in a loop. CHECK and EXIT, on
the other hand, are context-sensitive. Within a loop, they only apply to the execution of the loop
itself. Outside of a loop, they terminate the entire processing block in which they occur
(subroutine, dialog module, event block, and so on).
CONTINUE, CHECK, and EXIT can be used in all four loop types in ABAP (DO, WHILE, LOOP,
and SELECT).
Terminating a Loop Pass Unconditionally
To terminate a single loop pass immediately and unconditionally, use the CONTINUE statement
in the statement block of the loop.
After the statement, the system ignores any remaining statements in the current statement block,
and starts the next loop pass.
DO 4 TIMES.
IF SY-INDEX = 2.
CONTINUE.
ENDIF.
WRITE SY-INDEX.
ENDDO.
The output is:
1 3 4
The second loop pass is terminated without the WRITE statement being processed.
Terminating a Loop Pass Conditionally
To terminate a single loop pass conditionally, use the CHECK <condition> statement in the
statement block of the loop.
If the condition is not true, any remaining statements in the current statement block after the
CHECK statement are ignored, and the next loop pass starts. <condition> can be any logical
expression [Page 225].
DO 4 TIMES.
CHECK SY-INDEX BETWEEN 2 and 3.
WRITE SY-INDEX.
ENDDO.
The output is:
2 3
The first and fourth loop passes are terminated without the WRITE statement being
processed, because SY-INDEX is not between 2 and 3.
Exiting a Loop
To terminate an entire loop immediately and unconditionally, use the EXIT statement in the
statement block of the loop.
After this statement, the loop is terminated, and processing resumes after the closing statement
of the loop structure (ENDDO, ENDWHILE, ENDLOOP, ENDSELECT). In nested loops, only the
current loop is terminated.
DO 4 TIMES.
IF SY-INDEX = 3.
EXIT.
ENDIF.
WRITE SY-INDEX.
ENDDO.
The output is:
1 2
In the third loop pass, the loop is terminated before the WRITE statement is
processed.
null
‎2006 Nov 16 12:03 PM
Terminating Loops
ABAP contains termination statements that allow you to terminate a loop prematurely. There are
two categories of termination statement - those that only apply to the loop, and those that apply
to the entire processing block in which the loop occurs. The STOP and REJECT statements
belong to the latter group, and are described in more detail under Leaving Event Blocks [Page
966].
The termination statements that apply only to the loop in which they occur are CONTINUE,
CHECK, and EXIT. You can only use the CONTINUE statement in a loop. CHECK and EXIT, on
the other hand, are context-sensitive. Within a loop, they only apply to the execution of the loop
itself. Outside of a loop, they terminate the entire processing block in which they occur
(subroutine, dialog module, event block, and so on).
CONTINUE, CHECK, and EXIT can be used in all four loop types in ABAP (DO, WHILE, LOOP,
and SELECT).
Terminating a Loop Pass Unconditionally
To terminate a single loop pass immediately and unconditionally, use the CONTINUE statement
in the statement block of the loop.
After the statement, the system ignores any remaining statements in the current statement block,
and starts the next loop pass.
DO 4 TIMES.
IF SY-INDEX = 2.
CONTINUE.
ENDIF.
WRITE SY-INDEX.
ENDDO.
The output is:
1 3 4
The second loop pass is terminated without the WRITE statement being processed.
Terminating a Loop Pass Conditionally
To terminate a single loop pass conditionally, use the CHECK <condition> statement in the
statement block of the loop.
If the condition is not true, any remaining statements in the current statement block after the
CHECK statement are ignored, and the next loop pass starts. <condition> can be any logical
expression [Page 225].
DO 4 TIMES.
CHECK SY-INDEX BETWEEN 2 and 3.
WRITE SY-INDEX.
ENDDO.
The output is:
2 3
The first and fourth loop passes are terminated without the WRITE statement being
processed, because SY-INDEX is not between 2 and 3.
Exiting a Loop
To terminate an entire loop immediately and unconditionally, use the EXIT statement in the
statement block of the loop.
After this statement, the loop is terminated, and processing resumes after the closing statement
of the loop structure (ENDDO, ENDWHILE, ENDLOOP, ENDSELECT). In nested loops, only the
current loop is terminated.
DO 4 TIMES.
IF SY-INDEX = 3.
EXIT.
ENDIF.
WRITE SY-INDEX.
ENDDO.
The output is:
1 2
In the third loop pass, the loop is terminated before the WRITE statement is
processed.
null
‎2006 Nov 16 12:06 PM
Hi,
1) EXIT if in a loop endloop exits the loop, if in a procedure then exits the procedure. Any where else it exits the program.
2) STOP if executed in an executable program then its exits the processing block and then goes to the END-OF-SELECTION block.
3) CONTINUE can only be used in loops, this statement is used to stop the current loop pass and proceed with the next loop pass.
4) CHECK in a loop will stop the current loop pass and goes to the next loop pass if the logical condition is false else does nothing.
Regards,
Sesh
Message was edited by:
Seshatalpasai Madala
‎2006 Nov 17 12:03 AM