Saturday, 4 January 2014

Do Loops in SAP ABAP

Do Loops in SAP ABAP

The DO loop is a simple statement, here declare DO. Add a WRITE statement, and then ENDDO

While 'Hello':

You will notice there is nothing to tell the loop to end. If one tries to execute the code, the program will get stuck in a continuous loop endlessly writing out ‘Hello’ to the output screen. The transaction must be stopped and the code amended. A mechanism must be added to the DO loop to tell it when to stop processing the code inside it. Here, the TIMES addition is used. Amend the code as follows so that the system knows the loop is to be processed 15 times. Also here a ‘new line’ has been added before ‘Hello’:

WRITE: / 'Hello'.
Do Loops in SAP ABAP

The DO statement is useful for repeating a particular task a specific number of times. Just remember to always include the TIMES addition.
Now try some processing with the DO loop. Create a DATA variable named ‘a’, of type in-teger, and set the value of this to 0. Then, inside the DO loop, include the simple calcula-tion “a = a + 1”

Do Loops in SAP ABAP

The system also contains its own internal counter for how many times a DO loop is exe-cuted, which can be seen when this is executed in debug mode. Set a breakpoint on the DO line, then execute the code, keeping an eye on the ‘a’ field in the Field names section, and also includes ‘sy-index’ in one of these fields. You will see that ‘a’ keeps a running count of how many times the DO loop executes as well as the system variable sy-index. The values will be the same for both, going up by 1 each time the loop completes. The sy-index variable will in fact update a line of code before the ‘a’ variable, as it counts the DO loops, and the ‘a’ refers to the calculation on the next line of code

Do Loops in SAP ABAP

Note that here the blue arrow cursor has moved down a line in the second image, execut-ing the next line of code. If one adds a new line to the WRITE statement in the initial code, the output window will appear like this.
Do Loops in SAP ABAP

1 comment:

  1. Hi,
    It is very nice article which is having much information. The way of your presentation is too god and simple .The content is so useful . Oracle Financials training
    Thank you,