SAP LOOP AT ITAB STEP ABEXA



Get Example source ABAP code based on a different SAP table
  



ABAP_ITAB - Loop with Step Size
The example demonstrates the execution of the statement LOOP AT itab with different STEP additions.

ABAP_SOURCE_CODE
DEMO CL_DEMO_LOOP_AT_ITAB_USING_STP

ABAP_DESCRIPTION
The table itab is a hashed table with a unique key and a non-unique sorted key. Using STEP additions, the step size and the direction of a loop can be defined. The first table output displays the values for the hashed table in the order in which the table is filled. The LOOP is performed without a key specification and without a step size. The table output is in the order in which the table is filled and with the sy-tabix of 0. Each table line is read because the step size is automatically interpreted as 1. The LOOP is performed without a key specification and with the step size of -1. The table output is in the reverse order in which the table is filled and with the sy-tabix of 0. The LOOP is performed without a key specification and with the step size of 2. The table output is in the order in which the table is filled and with the sy-tabix of 0. Every second table line is read, starting with the first table line. The LOOP is performed without a key specification and with the step size of -2. The table output is in the reverse order in which the table is filled and with the sy-tabix of 0. Every second table line is read, starting with the last table line. The LOOP is performed with the sorted key sec_key and without a step size. The table output is in a sorted order from the lowest to the highest number and with the value of sy-tabix displaying the number of the respective table line in the secondary table index of sec_key. Each table line is read because the step size is automatically interpreted as 1. The LOOP is performed with the sorted key sec_key and with the step size of -1. The table output is in a reverse sorted order from the highest to the lowest number and with the value of sy-tabix displaying the number of the respective table line in the secondary table index of sec_key. The LOOP is performed with the sorted key sec_key and with the step size of 2. The table output is in a sorted order from the lowest to the highest number and with the value of sy-tabix displaying the number of the respective table line in the secondary table index of sec_key. Every second table line is read, starting with the first table line. The LOOP is performed with the sorted key sec_key and with the step size of -2. The table output is in a reverse sorted order from the highest to the lowest number and with the value of sy-tabix displaying the number of the respective table line in the secondary table index of sec_key. Every second table line is read, starting with the last table line. The LOOP is performed with the sorted key sec_key, with the step size of 1, and with the addition of FROM 1 TO 3. The table output is in a sorted order from table line 1 to 3 and with the value of sy-tabix displaying the number of the respective table line in the secondary table index of sec_key. Each table line is read, starting with the first table line. The LOOP is performed with the sorted key sec_key, with the step size of -1, and with the addition of FROM 3 TO 1. The table output is in a reverse sorted order from table line 1 to 3 and with the value of sy-tabix displaying the number of the respective table line in the secondary table index of sec_key. Each table line is read, starting with the last table line.
See also the executable example for the FOR expression using the addition STEP.