Loops in ABAP #
Infinite Loop - SM50 #
Objectives #
- Understand what an infinite loop is and why it occurs
- Identify risks associated with loops without exit conditions (DUMP TIMEOUT)
- Learn to manually interrupt an infinite loop via the /oSM50 transaction
- Always provide an exit condition to avoid program blockages
Definition #
An infinite loop occurs when a loop instruction (DO, WHILE) has no exit condition. The program continues to execute indefinitely, which can result in a DUMP of type TIMEOUT.
Walking in circles in a corridor without ever reaching the exit.
Infinite loops can block SAP and generate a DUMP TIMEOUT. Always provide an exit condition in your loops.
Example #
DO.
WRITE:/ SY-INDEX.
ENDDO.
- Here, the DO loop has no exit condition
- SY-INDEX displays indefinitely
- After a certain time, SAP automatically interrupts the program (DUMP TIMEOUT)
How to Interrupt an Infinite Loop #
- Open a new SAP session and execute the transaction
/oSM50 - Identify the blocked program in the list (column “Progr.”) and check the user (“User name”)
- Click on the program and select Administration → Program → Interrupt
- Confirm the alert message by answering “Yes”
- Refresh the screen (press [F8] or click the refresh button) to verify that the program has stopped
This procedure does not fix the loop in the code, it simply stops the running program. You must then correct the code by adding an exit condition.
Prevention - Best Practices #
DO Loop with Fixed Iteration #
DO 10 TIMES.
WRITE:/ 'Iteration', SY-INDEX.
ENDDO.
This loop will execute exactly 10 times and stop automatically.
DO Loop with EXIT Condition #
DO.
IF SY-INDEX > 10.
EXIT.
ENDIF.
WRITE:/ 'Iteration', SY-INDEX.
ENDDO.
The loop stops when SY-INDEX exceeds 10.
WHILE Loop with Condition #
DATA lv_index TYPE i VALUE 1.
WHILE lv_index <= 10.
WRITE:/ 'Number', lv_index.
lv_index = lv_index + 1.
ENDWHILE.
The loop stops when lv_index exceeds 10.
Using CHECK to Filter Iterations #
DO 100 TIMES.
CHECK SY-INDEX MOD 2 = 0.
WRITE:/ 'Even number:', SY-INDEX.
ENDDO.
Only even numbers are displayed, but the loop always ends after 100 iterations.
Common Mistakes to Avoid #
Mistake 1: DO Without Exit Condition #
* BAD - Infinite loop
DO.
WRITE:/ 'This will never stop'.
ENDDO.
Mistake 2: WHILE with Unmodified Condition #
* BAD - Infinite loop
DATA lv_flag TYPE abap_bool VALUE abap_true.
WHILE lv_flag = abap_true.
WRITE:/ 'This will never stop'.
ENDWHILE.
lv_flag is never changed, so the loop never exits.
Mistake 3: WHILE with Failed Condition Update #
* BAD - Infinite loop
DATA lv_index TYPE i VALUE 1.
WHILE lv_index < 10.
WRITE:/ 'This will never stop'.
* lv_index is never incremented!
ENDWHILE.
SM50 Transaction - Step by Step #
Step 1: Access SM50 #
- Log in to SAP with a different user (or different session)
- In the command field, type
/oSM50and press Enter
Step 2: Locate the Blocked Program #
- Look for your program in the “Progr.” column
- Verify it matches your username in the “User” column
- Note the process ID (PID) if needed
Step 3: Interrupt the Program #
- Select the row containing your program
- From the menu bar: Administration → Program → Interrupt
- Or use the keyboard shortcut if available
- Confirm when prompted
Step 4: Verify the Interruption #
- Press [F8] or click the refresh button
- The program should disappear from the list or change status
Monitoring Tools #
SM37 - Job Log #
Monitor background jobs and their execution status.
ST03N - Workload Analysis #
Track system performance and identify long-running programs.
SE30 - ABAP Trace #
Analyze program performance and identify performance bottlenecks.
Summary #
- Always provide an exit condition for a loop
- Loops without exit can cause DUMP TIMEOUT and block execution
SM50allows you to manually interrupt a blocked program- Tip: Use
EXIT,CHECK, orSY-INDEXto control loop duration- Always test your loops with small limits before running them on real data
Prevention is better than cure: always ensure your loops have clear exit conditions before deploying to production.
DO ENDDO - Loop Control in ABAP #
Objectives #
- Understand how the DO … ENDDO loop works
- Master control instructions: CHECK, EXIT, CONTINUE
- Know how to create fixed and dynamic loops
- Understand the use of SY-INDEX
- Identify and avoid infinite loops
- Apply best practices for structure and readability
Definition #
The DO … ENDDO instruction executes a block of instructions repeatedly. It can be limited by a number of iterations (n TIMES) or controlled dynamically with internal conditions (CHECK, EXIT, CONTINUE).
A DO loop is like a series of laps around a track. You can set the number of laps in advance, or decide along the way when to stop.
A loop without an exit condition can block the program. Always provide a way to stop: EXIT or end condition.
General Syntax #
DO [n TIMES].
[instruction_block]
ENDDO.
n→ number of iterations (optional)instruction_block→ instructions executed at each pass
If
nis specified, the loop executes exactlyntimes. Withoutn, it becomes potentially infinite and must be controlled by an exit condition.
SY-INDEX is an internal automatic counter that indicates the current iteration number (starts at 1).
Simple DO … ENDDO #
WRITE:/ ' - SIMPLE DO ... ENDDO...'.
DO 5 TIMES.
WRITE:/ 'Iteration', SY-INDEX.
ENDDO.
The loop executes five times and displays the iteration number at each pass.
To repeat a simple block a fixed number of times.
DO with Exit Condition #
EXIT immediately interrupts the loop, regardless of the current position.
WRITE:/ ' - DO WITH EXIT CONDITION...'.
DO.
IF SY-INDEX > 5.
EXIT.
ENDIF.
WRITE:/ 'Round', SY-INDEX.
ENDDO.
The loop has no fixed limit in advance, but stops as soon as SY-INDEX exceeds 5.
Do not combine EXIT without a clear condition. Always check a variable or threshold.
You run until a condition is reached (e.g., “I stop after 5 laps”).
DO with Internal Control (CHECK) #
CHECK verifies a condition and skips the rest of the loop if it is not met. It allows you to filter iterations.
WRITE:/ ' - DO WITH INTERNAL CONTROL (CHECK)...'.
DO 10 TIMES.
CHECK SY-INDEX MOD 2 = 0.
WRITE:/ 'Even number:', SY-INDEX.
ENDDO.
Only iterations where SY-INDEX is even are executed. The others are silently ignored.
CHECK is an elegant alternative to long IF … ELSE … ENDIF structures.
To ignore iterations that do not meet a logical criterion.
DO with CONTINUE #
CONTINUE skips the rest of the current block and moves directly to the next iteration.
WRITE:/ ' - DO WITH CONTINUE...'.
DO 10 TIMES.
IF SY-INDEX = 5.
CONTINUE.
ENDIF.
WRITE:/ 'Iteration', SY-INDEX.
ENDDO.
Iteration 5 is skipped, all others are executed.
CONTINUE does not exit the loop, it simply ignores the rest of the current block. Use EXIT if complete loop exit is desired.
You skip a lap without leaving the race.
Combined Examples #
WRITE:/ ' - COMBINED DO...'.
DO.
IF SY-INDEX > 10.
EXIT.
ENDIF.
CHECK SY-INDEX MOD 2 = 0.
WRITE:/ 'Even:', SY-INDEX.
ENDDO.
Infinite loop controlled dynamically. It stops at 10 and displays only even numbers.
Exercises #
1 - Fixed Loop #
Display numbers from 1 to 10 with DO … ENDDO.
Solution
DO 10 TIMES.
WRITE:/ SY-INDEX.
ENDDO.
2 - Infinite Loop with EXIT #
Create a loop without n TIMES and stop it when SY-INDEX reaches 7.
Solution
DO.
IF SY-INDEX > 7.
EXIT.
ENDIF.
WRITE:/ SY-INDEX.
ENDDO.
3 - Use CHECK #
Display only even numbers from 1 to 10.
Solution
DO 10 TIMES.
CHECK SY-INDEX MOD 2 = 0.
WRITE:/ SY-INDEX.
ENDDO.
4 - Use CONTINUE #
Display numbers from 1 to 10, skipping number 5.
Solution
DO 10 TIMES.
IF SY-INDEX = 5.
CONTINUE.
ENDIF.
WRITE:/ SY-INDEX.
ENDDO.
5 - Complete Combination #
Display even numbers up to 12, but stop the loop at 10.
Solution
DO 12 TIMES.
IF SY-INDEX > 10.
EXIT.
ENDIF.
CHECK SY-INDEX MOD 2 = 0.
WRITE:/ 'Even number:', SY-INDEX.
ENDDO.
Summary #
- DO … ENDDO → executes a block multiple times
- EXIT → quits the loop immediately
- CHECK → skips iterations that do not meet a condition
- CONTINUE → moves to the next iteration
- SY-INDEX → internal automatic counter
[!TIP] A race with multiple laps
- DO → main loop
- EXIT → quit the race
- CHECK → only run valid laps
- CONTINUE → skip a lap
- SY-INDEX → counter of the number of laps completed
WHILE ENDWHILE - Conditional Loop Control #
Objectives #
- Understand how WHILE … ENDWHILE works
- Know how to structure a logical condition to avoid infinite loops
- Differentiate between WHILE and DO … ENDDO
- Use CHECK, EXIT, CONTINUE inside a WHILE loop
- Design conditional loops with dynamic flow management
- Identify common errors and their causes
Definition #
The WHILE instruction executes a block of instructions as long as a logical condition is true. At each iteration, the condition is re-evaluated. When it becomes false, the loop stops.
You pour water as long as the glass is not full. As soon as the glass overflows, you stop pouring.
If the condition variable is not modified inside the loop, the loop becomes infinite.
General Syntax #
WHILE log_exp.
[instruction_block]
ENDWHILE.
log_exp→ logical condition for continuationinstruction_block→ instructions executed as long as the condition is true
The condition check is done before each iteration. If it is false from the start, the loop never executes.
To process sequences where the number of iterations depends on a dynamic condition.
Difference with DO … ENDDO #
| Instruction | Control | Type of Exit | Risk |
|---|---|---|---|
| DO … ENDDO | Fixed (n TIMES or EXIT) | Internal | Infinite loop if no condition |
| WHILE … ENDWHILE | Dynamic (logical condition) | External | Infinite loop if condition not modified |
DO counts laps, WHILE observes a condition.
Simple WHILE #
DATA lv_index TYPE i VALUE 1.
WHILE lv_index <= 5.
WRITE:/ 'Iteration', lv_index.
lv_index = lv_index + 1.
ENDWHILE.
The loop executes as long as lv_index is less than or equal to 5. It naturally stops when the condition becomes false.
You repeat an action as long as a constraint is not reached (here, the limit of 5).
WHILE with EXIT #
EXIT allows you to prematurely interrupt a loop, regardless of the exit condition.
DATA lv_index TYPE i VALUE 1.
WHILE lv_index <= 10.
IF lv_index > 5.
EXIT.
ENDIF.
WRITE:/ 'Round', lv_index.
lv_index = lv_index + 1.
ENDWHILE.
Even though the outer condition (lv_index <= 10) remains true, the loop stops when lv_index exceeds 5.
Use EXIT sparingly. It should remain readable and justified.
WHILE with CHECK #
CHECK verifies an internal condition. If the condition is false, the current iteration stops immediately.
DATA lv_index TYPE i VALUE 0.
WHILE lv_index < 10.
lv_index = lv_index + 1.
CHECK lv_index >= 3.
WRITE:/ 'Value:', lv_index.
ENDWHILE.
The first two values (1 and 2) are ignored. From 3 onwards, the loop executes the rest of the block.
Simplifies reading when you want to ignore certain cases without nesting multiple conditions.
WHILE with CONTINUE #
CONTINUE skips the rest of the current block and moves to the next iteration.
DATA lv_index TYPE i VALUE 0.
WHILE lv_index < 10.
lv_index = lv_index + 1.
IF lv_index MOD 2 = 0.
CONTINUE.
ENDIF.
WRITE:/ 'Odd:', lv_index.
ENDWHILE.
Even values are ignored, only odd values are displayed.
CONTINUE does not exit the loop, it only moves to the next iteration.
Like a player who skips a turn without leaving the game.
Complete Combination #
DATA lv_index TYPE i VALUE 0.
WHILE lv_index < 15.
lv_index = lv_index + 1.
CHECK lv_index > 3.
IF lv_index MOD 2 = 0.
CONTINUE.
ENDIF.
IF lv_index > 10.
EXIT.
ENDIF.
WRITE:/ 'Number processed:', lv_index.
ENDWHILE.
- CHECK ignores values up to 3
- CONTINUE skips even numbers
- EXIT interrupts the loop after 10 Result: 5, 7, 9
Exercises #
1 - Simple Loop #
Display numbers from 1 to 10 with a WHILE loop.
Solution
DATA lv_index TYPE i VALUE 1.
WHILE lv_index <= 10.
WRITE:/ lv_index.
lv_index = lv_index + 1.
ENDWHILE.
2 - Loop with CONTINUE #
Display numbers from 1 to 10, skipping multiples of 3.
Solution
DATA lv_index TYPE i VALUE 0.
WHILE lv_index < 10.
lv_index = lv_index + 1.
IF lv_index MOD 3 = 0.
CONTINUE.
ENDIF.
WRITE:/ lv_index.
ENDWHILE.
3 - Loop with CHECK #
Display only numbers greater than or equal to 5.
Solution
DATA lv_index TYPE i VALUE 0.
WHILE lv_index < 10.
lv_index = lv_index + 1.
CHECK lv_index >= 5.
WRITE:/ lv_index.
ENDWHILE.
4 - Loop with EXIT #
Display numbers from 1 to 20 and stop the loop when lv_index reaches 8.
Solution
DATA lv_index TYPE i VALUE 1.
WHILE lv_index <= 20.
IF lv_index > 8.
EXIT.
ENDIF.
WRITE:/ lv_index.
lv_index = lv_index + 1.
ENDWHILE.
5 - Complete Loop (CHECK + CONTINUE + EXIT) #
Display odd numbers starting from 3, but stop after 11.
Solution
DATA lv_index TYPE i VALUE 0.
WHILE lv_index < 20.
lv_index = lv_index + 1.
CHECK lv_index >= 3.
IF lv_index MOD 2 = 0.
CONTINUE.
ENDIF.
IF lv_index > 11.
EXIT.
ENDIF.
WRITE:/ lv_index.
ENDWHILE.
Summary #
- WHILE … ENDWHILE → executes as long as the condition remains true
- EXIT → quits the loop immediately
- CHECK → skips iterations that do not meet a condition
- CONTINUE → moves to the next iteration
- The condition must be modified at each turn to avoid infinite loops
[!TIP] Filling a glass
- WHILE → as long as the glass is not full
- EXIT → stop pouring even if the glass is not yet full
- CHECK → do not pour if the glass is already too hot
- CONTINUE → skip a glass and move to the next one