Implementing Loops

Objective

After completing this lesson, you will be able to Implement loops.

Implement Loops

Keywords DO, ENDDO, LOOP, and ENDLOOP can be used to define 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.

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

Business Example

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

Steps

  1. Create a new ABAP class ZCL_S4D100_XX_DO_ENDDO with interface IF_OO_ADT_CLASSRUN.

    1. Create a new ABAP class with the name, ZCL_S4D100_XX_ENDDO and add interface IF_OO_ADT_CLASSRUN.

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

  2. Activate the application.

    1. Press Activate (Ctrl+F3).

  3. In the METHOD IF_OO_ADT_CLASSRUN~MAIN, define a LOOP that iterates 3 times and outputs a message,

    1. Implement the following code in METHOD IF_OO_ADT_CLASSRUN~MAIN.

      Code Snippet
      Copy code
      Switch to dark mode
      123456789
      CONSTANTS: c_number0 TYPE i VALUE 3. out->write( '---------------------------------' ). out->write( 'Example 1: DO...ENDDO with TIMES' ). out->write( '---------------------------------' ). DO c_number0 TIMES. out->write( 'Hello World' ). ENDDO.
  4. Activate and test the class.

    1. Press Activate (Ctrl+F3).

    2. Press F9 to run the class.

    Practice

Implement a DO – ENDDO Loop with an Abort Condition

Business Example

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

Note

In this exercise, XX refers to your number.

Steps

  1. Copy your class ZCL_S4D100_XX_DO_ENDDO to ZCL_S4D100_XX_EXIT.

    1. Right-click on CLASS ZCL_S4D100_XX_DO_ENDDO in the Project Explorer and choose Duplicate.

    2. Enter your package and name and new class name ZCL_S4D100_XX_EXIT and press Next.

    3. Add it to your transport request and press Finish.

  2. Activate the application.

    1. Press Activate (Ctrl+F3).

  3. Change the code in if_oo_adt_classrun~main to set a new integer lv_number0 to a value of c_number0 * c_number0. Create a DO loop and subtract 1 from lv_number0 during each iteration of the loop. Exit the loop when lv_number0 is equal to c_number0.

    1. Implement the METHOD IF_OO_ADT_CLASSRUN~MAIN. with the following code:

      Code Snippet
      Copy code
      Switch to dark mode
      123456789101112131415
      CONSTANTS: c_number0 TYPE i VALUE 3. DATA: lv_number0 TYPE i. lv_number0 = c_number0 * c_number0. * Count backwards from lv_number0 to c_number0 DO. out->write( |{ sy-index }: Value of lv_number0: { lv_number0 }| ). lv_number0 = lv_number0 - 1. * Abort condition IF lv_number0 <= c_number0. "If 3 = 3 EXIT. ENDIF. ENDDO.
  4. Activate and test the program.

    1. Press Activate (Ctrl+F3).

    2. Press F9 to run the class.

    Practice

Log in to track your progress & complete quizzes