Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

stop,exit,check,

Former Member
0 Likes
1,686

hi,

please tell me the behavior of STOP,EXIT,CHECK. at various level, means in loop, in event..etc.

Thanks

Ramprasad D

5 REPLIES 5
Read only

Former Member
0 Likes
1,604

Hi Ramprasad,

In a loop, a statement block is executed several times in succession. There are four kinds of loops in ABAP:

Unconditional loops using the DO statement.

Conditional loops using the WHILE statement.

Loops through internal tables and extract datasets using the LOOP statement.

Loops through datasets from database tables using the SELECT statement.

This section deals with DO and WHILE loops. SELECT is an Open SQL statement, and is described in the Open SQL section. The LOOP statement is described in the sections on internal tables and extract datasets.

Unconditional Loops

To process a statement block several times unconditionally, use the following control structure:

DO [<n> TIMES] [VARYING <f> FROM <f1> NEXT <f 2>].

<Statement block>

ENDDO.

If you do not specify any additions, the statement block is repeated until it reaches a termination statement such as EXIT or STOP (see below). The system field SY-INDEX contains the number of loop passes, including the current loop pass.

Use the TIMES addition to restrict the number of loop passes to <n>. <n> can be literal or a variable. If <n> is 0 or negative, the system does not process the loop. If you do not use the TIMES option, you must ensure that the loop contains at least one EXIT or STOP statement to avoid endless loops.

You can assign new values to a variable <f> in each loop pass by using the VARYING option. You can use the VARYING addition more than once in a DO statement. <f 1 > and <f 2 > are the first two fields of a sequence of fields the same distance apart in memory and with the same type and length. In the first loop pass, <f> takes the value <f 1 >, in the second loop pass, <f 2 >, and so on. If you change the value of the field <f> within the DO loop, the value of the current field <f i > is also changed. You must ensure that there are not more loop passes than fields in the sequence, otherwise a runtime error occurs.

You can nest DO loops and combine them with other loop forms.

Simple example of a DO loop:

DO.

WRITE SY-INDEX.

IF SY-INDEX = 3.

EXIT.

ENDIF.

ENDDO.

The output is:

1 2 3

The loop is processed three times. Here, the processing passes through the loop three times and then leaves it after the EXIT statement.

Example of two nested loops with the TIMES addition:

DO 2 TIMES.

WRITE SY-INDEX.

SKIP.

DO 3 TIMES.

WRITE SY-INDEX.

ENDDO.

SKIP.

ENDDO.

The output is:

1

1 2 3

2

1 2 3

The outer loop is processed twice. Each time the outer loop is processed, the inner loop is processed three times. Note that the system field SY-INDEX contains the number of loop passes for each loop individually.

Example of the VARYING addition in a DO loop:

DATA: BEGIN OF TEXT,

WORD1(4) VALUE 'This',

WORD2(4) VALUE 'is',

WORD3(4) VALUE 'a',

WORD4(4) VALUE 'loop',

END OF TEXT.

DATA: STRING1(4), STRING2(4).

DO 4 TIMES VARYING STRING1 FROM TEXT-WORD1 NEXT TEXT-WORD2.

WRITE STRING1.

IF STRING1 = 'is'.

STRING1 = 'was'.

ENDIF.

ENDDO.

SKIP.

DO 2 TIMES VARYING STRING1 FROM TEXT-WORD1 NEXT TEXT-WORD3

VARYING STRING2 FROM TEXT-WORD2 NEXT TEXT-WORD4.

WRITE: STRING1, STRING2.

ENDDO.

The output is:

This is a loop

This was a loop

The structure TEXT represents a series of four equidistant fields in memory. Each time the first DO loop is processed, its components are assigned one by one to STRING1. Whenever STRING1 contains ‘is’, its contents are changed to ‘was’. This also changes TEXT-WORD2 to ‘was’. Each time the second DO loop is processed, the components of TEXT are passed to STRING1 and to STRING2.

Conditional Loops

To repeat a statement block for as long as a certain condition is true, use the following control structure:

WHILE <condition> [VARY <f> FROM <f1> NEXT <f 2>].

<statement block>

ENDWHILE.

<condition> can be any logical expression. The statement block between WHILE and ENDWHILE is repeated as long as the condition is true or until a termination statement such as EXIT or STOP occurs. The system field SY-INDEX contains the number of loop passes, including the current loop pass. The VARY option of the WHILE statement works in the same way as the VARYING option of the DO statement (see above).

To avoid endless loops, you must ensure that the condition of a WHILE statement can be false, or that the statement block contains a termination statement such as EXIT or STOP.

You can nest WHILE loops to any depth, and combine them with other loop forms.

DATA: LENGTH TYPE I VALUE 0,

STRL TYPE I VALUE 0,

STRING(30) TYPE C VALUE 'Test String'.

STRL = STRLEN( STRING ).

WHILE STRING NE SPACE.

WRITE STRING(1).

LENGTH = SY-INDEX.

SHIFT STRING.

ENDWHILE.

WRITE: / 'STRLEN: ', STRL.

WRITE: / 'Length of string:', LENGTH.

The output appears as follows:

T e s t S t r i n g

STRLEN: 11

Length of String: 11

Here, a WHILE loop is used to determine the length of a character string. This is done by shifting the string one position to the left each time the loop is processed until it contains only blanks. This example has been chosen to demonstrate the WHILE statement. Of course, you can determine the length of the string far more easily and efficiently using the STRLEN function.

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.

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.

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.

reward pts if it usefull;

regards

Sathish:)

Read only

Former Member
0 Likes
1,604

Hi,

In a loop, a statement block is executed several times in succession. There are four kinds of loops in ABAP:

Unconditional loops using the DO statement.

Conditional loops using the WHILE statement.

Loops through internal tables and extract datasets using the LOOP statement.

Loops through datasets from database tables using the SELECT statement.

This section deals with DO and WHILE loops. SELECT is an Open SQL statement, and is described in the Open SQL section. The LOOP statement is described in the sections on internal tables and extract datasets.

Unconditional Loops

To process a statement block several times unconditionally, use the following control structure:

DO [<n> TIMES] [VARYING <f> FROM <f1> NEXT <f 2>].

<Statement block>

ENDDO.

If you do not specify any additions, the statement block is repeated until it reaches a termination statement such as EXIT or STOP (see below). The system field SY-INDEX contains the number of loop passes, including the current loop pass.

Use the TIMES addition to restrict the number of loop passes to <n>. <n> can be literal or a variable. If <n> is 0 or negative, the system does not process the loop. If you do not use the TIMES option, you must ensure that the loop contains at least one EXIT or STOP statement to avoid endless loops.

You can assign new values to a variable <f> in each loop pass by using the VARYING option. You can use the VARYING addition more than once in a DO statement. <f 1 > and <f 2 > are the first two fields of a sequence of fields the same distance apart in memory and with the same type and length. In the first loop pass, <f> takes the value <f 1 >, in the second loop pass, <f 2 >, and so on. If you change the value of the field <f> within the DO loop, the value of the current field <f i > is also changed. You must ensure that there are not more loop passes than fields in the sequence, otherwise a runtime error occurs.

You can nest DO loops and combine them with other loop forms.

Simple example of a DO loop:

DO.

WRITE SY-INDEX.

IF SY-INDEX = 3.

EXIT.

ENDIF.

ENDDO.

The output is:

1 2 3

The loop is processed three times. Here, the processing passes through the loop three times and then leaves it after the EXIT statement.

Example of two nested loops with the TIMES addition:

DO 2 TIMES.

WRITE SY-INDEX.

SKIP.

DO 3 TIMES.

WRITE SY-INDEX.

ENDDO.

SKIP.

ENDDO.

The output is:

1

1 2 3

2

1 2 3

The outer loop is processed twice. Each time the outer loop is processed, the inner loop is processed three times. Note that the system field SY-INDEX contains the number of loop passes for each loop individually.

Example of the VARYING addition in a DO loop:

DATA: BEGIN OF TEXT,

WORD1(4) VALUE 'This',

WORD2(4) VALUE 'is',

WORD3(4) VALUE 'a',

WORD4(4) VALUE 'loop',

END OF TEXT.

DATA: STRING1(4), STRING2(4).

DO 4 TIMES VARYING STRING1 FROM TEXT-WORD1 NEXT TEXT-WORD2.

WRITE STRING1.

IF STRING1 = 'is'.

STRING1 = 'was'.

ENDIF.

ENDDO.

SKIP.

DO 2 TIMES VARYING STRING1 FROM TEXT-WORD1 NEXT TEXT-WORD3

VARYING STRING2 FROM TEXT-WORD2 NEXT TEXT-WORD4.

WRITE: STRING1, STRING2.

ENDDO.

The output is:

This is a loop

This was a loop

The structure TEXT represents a series of four equidistant fields in memory. Each time the first DO loop is processed, its components are assigned one by one to STRING1. Whenever STRING1 contains ‘is’, its contents are changed to ‘was’. This also changes TEXT-WORD2 to ‘was’. Each time the second DO loop is processed, the components of TEXT are passed to STRING1 and to STRING2.

Conditional Loops

To repeat a statement block for as long as a certain condition is true, use the following control structure:

WHILE <condition> [VARY <f> FROM <f1> NEXT <f 2>].

<statement block>

ENDWHILE.

<condition> can be any logical expression. The statement block between WHILE and ENDWHILE is repeated as long as the condition is true or until a termination statement such as EXIT or STOP occurs. The system field SY-INDEX contains the number of loop passes, including the current loop pass. The VARY option of the WHILE statement works in the same way as the VARYING option of the DO statement (see above).

To avoid endless loops, you must ensure that the condition of a WHILE statement can be false, or that the statement block contains a termination statement such as EXIT or STOP.

You can nest WHILE loops to any depth, and combine them with other loop forms.

DATA: LENGTH TYPE I VALUE 0,

STRL TYPE I VALUE 0,

STRING(30) TYPE C VALUE 'Test String'.

STRL = STRLEN( STRING ).

WHILE STRING NE SPACE.

WRITE STRING(1).

LENGTH = SY-INDEX.

SHIFT STRING.

ENDWHILE.

WRITE: / 'STRLEN: ', STRL.

WRITE: / 'Length of string:', LENGTH.

The output appears as follows:

T e s t S t r i n g

STRLEN: 11

Length of String: 11

Here, a WHILE loop is used to determine the length of a character string. This is done by shifting the string one position to the left each time the loop is processed until it contains only blanks. This example has been chosen to demonstrate the WHILE statement. Of course, you can determine the length of the string far more easily and efficiently using the STRLEN function.

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.

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.

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.

reward pts if it usefull;

Regards

Read only

Former Member
0 Likes
1,604

STOP

Syntax

STOP.

Effect

The statement STOP is only to be used in executable programs and in the following event blocks:

AT SELECTION-SCREEN (without additions)

START-OF-SELECTION

GET

You leave these event blocks via STOP, and the runtime environment triggers the event END-OF-SELECTION.

Note

The statement STOP is forbidden in methods and, since Release 6.10, leads to an uncatchable exception during the processing of screens called with CALL SCREEN and in programs not called with SUBMIT.

Example

Ending the event blocks GET sbook after max postings have been issued, and branching to END-OF-SELECTION.

NODES: sflight, sbook.

DATA: bookings TYPE i,

max TYPE i VALUE 100.

GET sflight.

WRITE: / sflight-carrid, sflight-connid, sflight-fldate.

GET sbook.

bookings = bookings + 1.

WRITE: / sbook-bookid, sbook-customid.

IF bookings = max.

STOP.

ENDIF.

END-OF-SELECTION.

ULINE.

WRITE: / 'First', bookings, 'bookings'.

***************************************************

EXIT - processing_block

Syntax

EXIT.

Effect

If the EXIT statement is executed outside of a loop, it will immediately terminate the current processing block.

After the prodessing block has been executed, the runtime environment behaves in such a way that it follows with the exception Reporting Event Blocks START-OF-SELECTION and GET the schema from Leave Processing Blocks.

After the Reporting Processing Blocks START-OF-SELECTION and GET have been exited using EXIT, the runtime environment does not trigger any further reporting events. Instead, it directly calls the list processor for displaying the basic list.

Note

We recommend using EXIT only within loops (see EXIT (loops) ). Instead, use RETURN to leave processing blocks.

*********************************************************

CHECK - processing_block

Variants:

1. CHECK log_exp.

2. CHECK SELECT-OPTIONS.

Effect

Conditional exiting of a processing block. There are two options for the conditional exiting of processing blocks using CHECK.

Variant 1

CHECK log_exp.

Effect

If the statement CHECK is executed outside a loop and log_exp is incorrect, the statement terminates the current process block. You can specify any logical expression for log_exp.

The behavior of the runtime environment after exiting the processing block is described under Exiting Processing Blocks.

Note

SAP recommends to use this procedure with the statement CHECK only inside loops (see CHECK (Loops) ).

Variant 2

CHECK SELECT-OPTIONS.

Effect

In this way, you can use CHECK only in event blocks for reporting events GET but not in methods. Regardless of whether the statement is executed inside or outside a loop, the effect is the same.

The statement checks whether the content of theinterface working area that was filled for the the current GET event by the logical database fullfills the conditions in all selection tables that are associated with the current node of the logical database. The name of the node is statically taken from the next highest GET statement in the program. The following restrictions apply:

The statement CHECK SELECT-OPTIONS is only executed when the type of the current node of the logical database is a database table.

If the node is to be used for unlimited selections, the statement only evaluates the selection criteria that have been declared with the extension NO DATABASE SELECTION of the statement SELECT-OPTIONS.

If the conditions in one of the selection tables are not fullfilled, the GET event block is exited. The behavior of the runtime environment is described under Exiting Processing Blocks.

Note

For performance reasons, the statement CHECK should only be used for checking the data selections during GET events if the selections provided by the logical database are not sufficient.

Example

Exiting a GET event block if the content of the components seatsmax and seatsocc of the interface working area sflight do not fullfill the conditions in the selection tables s_max or s_occ.

NODES sflight.

SELECT-OPTIONS: s_max FOR sflight-seatsmax,

s_occ FOR sflight-seatsocc.

GET sflight.

WRITE: / sflight-carrid, sflight-connid.

CHECK SELECT-OPTIONS.

WRITE: sflight-seatsmax, sflight-seatsocc.

Regards,

Pavan P.

Read only

Former Member
0 Likes
1,604

Hi,

STOP is used to stop the current processing and generally used to teminate th loops.

EXIT is used to exit the entire program.

CHECK is used to check the status of current line.

Pls do reward points.

Regards,

Ameet

Read only

Former Member
0 Likes
1,604

Hi,

stop/ Exit - Terminates the loop immediately. Processing continues at the statement immediately foollowing endloop.

Check - if exp is true processing continues as if this statement had not been executed. if exp is false its eggect is the same as continue.

Continue - goes immediately to the endloop statement, bypassing all following statements within loop. the next row is returened from intenal table and processing continues from the top of the loop. if there are no more rows to retrieve, processing continues at the first statement following endloop.

Reward if helpful

Regards

Raghavendra.D.S