ABAP - Step
Today a new addition for the loops and how you can use it. With Step you have the possibility to skip steps in a loop.
Table of contents
Today an extension of various expressions in ABAP to influence a step size and order. Step is not only valid for a statement, but now works for several expressions in ABAP. In this article we will go over a few rules. The new statements are possible from ABAP 7.56 or S/4 HANA 2021.
Preperation
For our examples, we fill the tables accordingly and prepare the data so that we can use them in the examples. We fill the table with the numbers 1 to 20 with a simple DO loop.
DO 20 TIMES.
INSERT sy-index INTO TABLE mt_data.
ENDDO.
Loop
In the article we will mainly deal with the simple loop with LOOP, since there are some special features to be considered here. If the STEP is not specified for a loop, then this is set to 1 as the default value, i.e. we loop forward with a step length of 1. The step can never be set to 0, since this would make no sense and the compiler immediately sees this as an error would spend.
Forward
Now let's give a positive value of 2 and in the loop print out the numbers that will be processed in the loop:
io_out->write( 'Loop with Step 2' ).
LOOP AT mt_data INTO DATA(ld_data) STEP 2.
io_out->write( |{ ld_data }| ).
ENDLOOP.
The output of the loop now looks like this:
Backward
In the next loop, we now enter a negative increment of -3 and output the result back to the console:
io_out->write( 'Loop with Step -3' ).
LOOP AT mt_data INTO DATA(ld_data) STEP -3.
io_out->write( |{ ld_data }| ).
ENDLOOP.
The console output now looks like this, with the loop beginning with the first entry from the end:
WHERE
In the next step we combine the step with the where condition in the loop. Only a step of 1 or -1 can be specified here, otherwise the compiler will issue an error message. Accordingly, we would iterate over the constrained result, either forward or backward.
io_out->write( 'Loop with Where' ).
LOOP AT mt_data INTO DATA(ld_data) STEP -1 WHERE table_line >= 10 AND table_line <= 15.
io_out->write( |{ ld_data }| ).
ENDLOOP.
The result after delimitation and loop now looks like this:
FROM/TO
The last example refers to the delimitation via the index with FROM and TO. It should be noted that if a range is delimited with both statements, the order from/to changes if a negative increment is used. In the example below, we start at 5 and go backwards to 1 (unspecified).
io_out->write( 'Loop with From/To' ).
LOOP AT mt_data INTO DATA(ld_data) FROM 5 STEP -2.
io_out->write( |{ ld_data }| ).
ENDLOOP.
In addition, the corresponding result of the console after the loop:
Full example
Here is the complete example again in the form of an executable class for copying or reading:
CLASS zcl_bs_demo_loop_step DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
TYPES:
tt_int TYPE STANDARD TABLE OF i WITH EMPTY KEY.
DATA:
mt_data TYPE tt_int.
PROTECTED SECTION.
PRIVATE SECTION.
METHODS:
prepare_table_with_data,
loop_forward
IMPORTING
io_out TYPE REF TO if_oo_adt_classrun_out,
loop_backward
IMPORTING
io_out TYPE REF TO if_oo_adt_classrun_out,
loop_with_where
IMPORTING
io_out TYPE REF TO if_oo_adt_classrun_out,
loop_with_from_to
IMPORTING
io_out TYPE REF TO if_oo_adt_classrun_out.
ENDCLASS.
CLASS zcl_bs_demo_loop_step IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
prepare_table_with_data( ).
loop_forward( out ).
out->write( '*****' ).
loop_backward( out ).
out->write( '*****' ).
loop_with_where( out ).
out->write( '*****' ).
loop_with_from_to( out ).
out->write( '*****' ).
ENDMETHOD.
METHOD prepare_table_with_data.
DO 20 TIMES.
INSERT sy-index INTO TABLE mt_data.
ENDDO.
ENDMETHOD.
METHOD loop_forward.
io_out->write( 'Loop with Step 2' ).
LOOP AT mt_data INTO DATA(ld_data) STEP 2.
io_out->write( |{ ld_data }| ).
ENDLOOP.
ENDMETHOD.
METHOD loop_backward.
io_out->write( 'Loop with Step -3' ).
LOOP AT mt_data INTO DATA(ld_data) STEP -3.
io_out->write( |{ ld_data }| ).
ENDLOOP.
ENDMETHOD.
METHOD loop_with_where.
io_out->write( 'Loop with Where' ).
LOOP AT mt_data INTO DATA(ld_data) STEP -1 WHERE table_line >= 10 AND table_line <= 15.
io_out->write( |{ ld_data }| ).
ENDLOOP.
ENDMETHOD.
METHOD loop_with_from_to.
io_out->write( 'Loop with From/To' ).
LOOP AT mt_data INTO DATA(ld_data) FROM 5 STEP -2.
io_out->write( |{ ld_data }| ).
ENDLOOP.
ENDMETHOD.
ENDCLASS.
Conclusion
To what extent the new extension of the expressions makes sense is certainly debatable. But in the end we get more options that already exist in other languages and it expands the spectrum in the ABAP language.