Tuesday, 7 January 2014

Loop Termination – CHECK in SAP ABAP

Loop Termination – CHECK in SAP ABAP

The CHECK statement works similarly to the CONTINUE statement, but this time allows you to check specific conditions. When the logic of a CHECK statement is defined, if the condition is not met, any remaining statements in the block will not be executed and processing will return to the top of the loop. It can be thought of as a combination of the IF and CONTINUE statements. To use the CHECK statement to achieve the same ends as in the example above, the syntax would look like this.
                                                 
Loop Termination – CHECK in SAP ABAP

The program will check that the sy-index field does not contain a value equal to 2, and where it does not, will continue executing the code. When it does contain 2, the condition attached will not be true and the CHECK statement will cause the loop to start again, miss-ing the WRITE statement. This can be executed in debug mode to closely observe how it works. The output window, once this is complete, will again appear like this.
                                              
Loop Termination – CHECK in SAP ABAP

When you are looking at programs created by other people, do not be surprised to see the CHECK statement used outside loops. It is not only used to terminate a loop pass, but can check, and terminate other processing blocks at any point if its particular conditions are not met. You must be aware of where the CHECK statement is being used, as putting it in the wrong place can even cause the entire program to terminate. For example here, the statement will only allow processing to continue if the value of ‘a’ is equal to 1. Since the value of ‘a’ equals 0, it will always terminate the program before the DO loop is reached.
Loop Termination – CHECK in SAP ABAP

1 comment: