Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Incrementing an integer inside loop in an ABAP program
To increment an integer inside a loop in an ABAP program, you have several methods available. The most common approaches involve using the ADD statement or direct assignment operations.
Methods for Incrementing Integers
Using ADD Statement
The most straightforward way to increment an integer is using the ADD statement ?
DATA: ls_id TYPE i VALUE 0. DO 5 TIMES. ADD 1 TO ls_id. WRITE: / 'Current value:', ls_id. ENDDO.
Using Assignment Operation
You can also increment using direct assignment. Note that proper spacing is required between the variable name and the plus operator ?
DATA: ls_id TYPE i VALUE 0. DO 5 TIMES. ls_id = ls_id + 1. WRITE: / 'Current value:', ls_id. ENDDO.
Using System Variables with Internal Tables
When working with internal tables, you can use system variables SY-TABIX and SY-INDEX depending on whether the loop is nested or not ?
DATA: lt_data TYPE TABLE OF string,
lv_counter TYPE i.
APPEND 'Item 1' TO lt_data.
APPEND 'Item 2' TO lt_data.
APPEND 'Item 3' TO lt_data.
LOOP AT lt_data INTO DATA(lv_item).
lv_counter = sy-tabix.
WRITE: / 'Index:', lv_counter, 'Item:', lv_item.
ENDLOOP.
SY-TABIX contains the current index of the table loop, while SY-INDEX contains the current iteration number in DO and WHILE loops.
Conclusion
Incrementing integers in ABAP loops can be accomplished using the ADD statement, direct assignment, or system variables for table operations. Choose the method that best fits your specific use case and coding standards.
