Implementing Loops

Objectives

After completing this lesson, you will be able to:

  • Implement loops in an ABAP program

Implement Loops

Loops in ABAP are defined in a block of code that is executed several times.

The simplest form of loops consists of a code block surrounded by statements DO – ENDDO. Without further measures, these statements could establish an endless loop that must be avoided by one of the following possibilities:

Specified Number of Loops
By extending the DO statement with an integer expression followed by keyword TIMES, you can specify explicitly how often the code block is to be processed. The integer expression can be as simple as a number literal, but also arithmetic calculations can be used as well. If the value of the expression equals 0, the code block between DO and ENDDO is not executed at all, and the program immediately continues with the code after the ENDDO.
Abort Based on a Logical Condition
You can abort a loop anytime using the EXIT statement. The program then continues with the code after ENDDO. Be aware that outside of the loop EXIT has a different effect. There it terminates the processing of the current processing block, for example, the current method.

Usually, EXIT is surrounded by IF and ENDIF to terminate the loop depending on an abort condition. Be aware that such loops can turn into endless loops if the abort condition is never true.

Of course, it is possible to combine the two techniques, that is, explicitly specify the number of loops and then leave the loop with EXIT. Therefore, the number of loops becomes a maximum number that might not be reached at runtime.

In the code block between DO and ENDDO, you can implement read-accesses to ABAP built-in data object SY-INDEX. This integer variable serves as a loop counter, that is, the ABAP runtime increases the variable by one at the beginning of each new loop.

Note
In contradiction to what you might be used to from other programming languages, sy-index starts with value 1 during the first iteration.

ABAP built-in variable SY-TABIX can fulfill a similar purpose for loops. But be aware that SY-TABIX is not really a counter, but it identifies the position of the table row that is processed in the current loop.

Implement a DO – ENDDO Loop in a Program

Business Example

Using the DO . . ENDDO keywords, users may want to create a loop with a specified number of loops.

Prerequisites

Steps

  1. Create a new ABAP class, add it to your package, and tie it to your transport request.

    1. Create a new ABAP class with the name, ZCL_S4D100_##_DO_ENDDO.

    2. Add it to your package and tie it to your transport request.

  2. Activate the application.

    1. Activate the application ZCL_S4D100_##_DO_ENDDO.

  3. Define the Interface (IF_OO_ADT_CLASSRUN) in the PUBLIC section.

    1. Implement the METHOD IF_OO_ADT_CLASSRUN~MAIN.

    2. Define a constant C_NUMBER0 of type I value 3.

    3. Use the DO…ENDDO Loop.

    4. Use the OUT->WRITE statement to output the text ‘Hello World’.

    5. Activate the program.

  4. Test the program and remove the output.

    1. Test the program by selecting F9 on your keyboard.

    2. Select the Clear Console button to remove the output.

    Practice

Implement a DO – ENDDO Loop with an Abort Condition in a Program

Business Example

Using the DO . . ENDDO keywords, users may want to create a loop with an abort condition using the keyword EXIT.

Prerequisites

Steps

  1. Copy the CLASS ZCL_S4D100_##_DO_ENDDO, add it to your package, and tie it to your transport request.

    1. Copy the CLASS ZCL_S4D100_##_DO_ENDDO using the name, ZCL_S4D100_##_EXIT.

    2. Add it to your package and tie it to your transport request.

  2. Activate the application.

    1. Activate the application, ZCL_S4D100_##_EXIT.

  3. Define the Interface (IF_OO_ADT_CLASSRUN) in the PUBLIC section.

    1. Implement the METHOD IF_OO_ADT_CLASSRUN~MAIN.

    2. Define a local variable LV_NUMBER0 of type I.

    3. Use the DO…ENDDO conditional branching.

    4. Use the OUT->WRITE statement to output each Loop value using the SY-INDEX and the local variable LV_NUMBER0.

    5. Use the IF. . .ENDIF condition to determine the value of the local variable LV_NUMBER0 and check if it meets the condition where it is equal to or less than 3. EXIT the IF. . .ENDIF and the DO. . .ENDDO loop if it is true.

    6. Add the following code to your program.

    7. Activate the program.

  4. Test the program and remove the output.

    1. Test the program by selecting F9 on your keyboard.

    2. Select the Clear Console button to remove the output.

    Practice

Log in to track your progress & complete quizzes