ABAP Keyword Documentation → ABAP − Reference → Program Flow Logic → Control Structures → Loops
WHILE
Other versions: 7.31 | 7.40 | 7.54
Syntax
WHILE log_exp
[statement_block]
ENDWHILE.
Effect
Conditional loop. The statements WHILE
and ENDWHILE
define a control structure that can contain a closed statement block statement_block
. After WHILE
, any
logical expression log_exp
can follow.
The statement block is repeated as long as the logical expression is true, or until it is exited with
one of the statements to leave loops. In particular, the statement
EXIT is ideal for exiting a loop completely. Within the statement block, the system field
sy-index
contains the number of previous loop passes, including the current pass. In nested loops, sy-index
always refers to the current loop.
Notes
- A maximum runtime can be configured using the profile parameters rdisp/scheduler/prio_low/max_runtime, rdisp/scheduler/prio_normal/max_runtime, and rdisp/scheduler/prio_high/max_runtime. If this runtime is exceeded because the logical expression is never false and the loop is not exited in any other way, the program is ended by the runtime environment. The profile parameter rdisp/max_wprun_time, on the other hand, is obsolete.
-
If
WHILE
loops are used to construct values or fill internal tables, they can probably be expressed more elegantly using conditional iterations withFOR
in constructor expressions. -
The obsolete addition
VARY
can be used to process a sequence of data objects in the memory.
Example
Fills an internal table with 100 rows.
DATA itab TYPE TABLE OF i.
WHILE lines( itab ) < 100.
itab = VALUE #( BASE itab ( sy-index ) ).
ENDWHILE.