- Docs
- Algorithms
- Hashing
ABAP Loops
Loops in ABAP (Advanced Business Application Programming) are control flow statements that allow you to repeatedly execute a block of code as long as a specific condition is met. Loops are fundamental to programming in ABAP, especially when dealing with collections like internal tables.
1. Types of Loops in ABAP
ABAP provides several types of loops to iterate over data structures:
- DO Loop: Executes a block of code a fixed number of times.
- WHILE Loop: Executes a block of code as long as a specified condition remains true.
- LOOP AT: Iterates over an internal table.
2. DO Loop
The DO
loop in ABAP is used to execute a block of code a specific number of times. It can also be terminated early using the EXIT
statement.
Syntax:
DO n TIMES.
"Your code here
ENDDO.
Example:
DATA: lv_counter TYPE i.
DO 5 TIMES.
lv_counter = lv_counter + 1.
WRITE: / 'Iteration:', lv_counter.
ENDDO.
In this example, the loop will run 5 times, incrementing and printing the counter value on each iteration.
3. WHILE Loop
The WHILE
loop in ABAP repeatedly executes a block of code as long as the given condition evaluates to true.
Syntax:
WHILE condition.
"Your code here
ENDWHILE.
Example:
DATA: lv_counter TYPE i VALUE 1.
WHILE lv_counter <= 5.
WRITE: / 'Counter:', lv_counter.
lv_counter = lv_counter + 1.
ENDWHILE.
This loop will continue to run as long as lv_counter
is less than or equal to 5.
4. LOOP AT
The LOOP AT
statement is used to iterate over internal tables. This is one of the most common loops in ABAP when working with collections of data.
Syntax:
LOOP AT itab INTO wa.
"Your code here
ENDLOOP.
Example:
DATA: lt_employee TYPE TABLE OF zemployee,
ls_employee TYPE zemployee.
LOOP AT lt_employee INTO ls_employee.
WRITE: / 'Employee ID:', ls_employee-empid,
'Name:', ls_employee-name.
ENDLOOP.
In this example, the loop iterates over each row in the internal table lt_employee
, processing the data in the work area ls_employee
.
5. Nested Loops
ABAP also allows for nested loops, where one loop is placed inside another. This is useful when you need to compare or process data across multiple collections.
Example:
DATA: lt_orders TYPE TABLE OF zorders,
lt_items TYPE TABLE OF zitems,
ls_order TYPE zorders,
ls_item TYPE zitems.
LOOP AT lt_orders INTO ls_order.
LOOP AT lt_items INTO ls_item WHERE order_id = ls_order-order_id.
WRITE: / 'Order:', ls_order-order_id, 'Item:', ls_item-item_id.
ENDLOOP.
ENDLOOP.
This example demonstrates how to loop through orders and their corresponding items by nesting loops.
6. Loop Control Statements
EXIT
The EXIT
statement is used to immediately terminate a loop.
CHECK
The CHECK
statement is used to skip the current iteration of the loop if a condition is not met.
CONTINUE
The CONTINUE
statement skips the remaining code in the current loop iteration and proceeds to the next iteration.
Example:
DATA: lt_numbers TYPE TABLE OF i,
lv_number TYPE i.
LOOP AT lt_numbers INTO lv_number.
CHECK lv_number MOD 2 = 0.
WRITE: / 'Even Number:', lv_number.
ENDLOOP.
In this example, only even numbers from the internal table lt_numbers
are processed and printed.
Conclusion
Loops are essential for repetitive tasks in ABAP, whether iterating over collections or repeating actions a certain number of times. Understanding how to use different types of loops and loop control statements allows you to write efficient and maintainable code in ABAP.