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 TIMES is suitable when the number of attempts is known or safely bounded.
  • WHILE is useful when continuing depends on a condition, but make sure the condition can eventually become false.
  • LOOP AT iterates 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

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.

Choose your ABAP environment

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

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.