Repetition needs a clear end
Suppose a scanner processes one item at a time until three units have been counted. A loop is useful when the same action repeats, but every loop needs a safe way to finish.
A bounded DO loop
DATA(result) = 0.
DO 10 TIMES.
result = result + 1.
IF result = 3.
EXIT.
ENDIF.
ENDDO.
DO 10 TIMES provides a maximum number of repetitions. EXIT leaves the loop as soon as the task is complete. In real code, the limit and stop condition should match the actual business rule.
Choosing a loop
DO n TIMESis suitable when the number of attempts is known or safely bounded.WHILEis useful when continuing depends on a condition, but make sure the condition can eventually become false.LOOP ATiterates through rows in an internal table; it is not a general-purpose counter loop.
For a simple fixed scan, the bounded loop is clearest. Avoid a WHILE whose controlling value never changes: it can run indefinitely.
Practice
Increment result in a bounded loop and use EXIT once the third simulated scan is reached. Add the mode-specific output statement. The checker checks statement structure, not program execution.
Sources
- SAP Samples, ABAP Cheat Sheets overview
- SAP Help Portal, ABAP Keyword Documentation
Version note: Confirm syntax and restrictions in the documentation for your target ABAP language version.
Developer practice · ABAP
Practice: stop after the needed quantity
Count simulated scans up to three in result and stop early with EXIT when the third unit is reached.
This choice changes the output pattern checked here: WRITE belongs in a classic executable report, while out->write( ) uses the supplied output reference in an ABAP Cloud class-run method. It does not switch the language version of the other code; check release and API availability in your target system.
Complete the task and include the output statement shown for your selected environment. Variable names can differ if you use them consistently. This browser checker checks structure only; it does not compile or run ABAP.
0 required structures detected · 0 checks
Write your solution, then check its structure.
One possible solution
DATA(result) = 0.
DO 10 TIMES.
result = result + 1.
IF result = 3.
EXIT.
ENDIF.
ENDDO.
WRITE result.out->write( result ).These are context-specific output examples. In your own program, use the result variable from your solution and the output mechanism available in that program.
Debug this code
A syntax error appears near ENDDO. Find the missing block terminator and repair the snippet.
Edit the code, then check for the expected repair. This is a structure check, not an ABAP compiler.
Find the issue, repair it, and check your change.
Diagnosis and repair
Diagnosis: The IF block is still open when ENDDO is reached. Close the IF with ENDIF before ending the loop.
DATA(result) = 0.
DO 10 TIMES.
result = result + 1.
IF result = 3.
EXIT.
ENDIF.
ENDDO.