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
- The profile parameter rdisp/max_wprun_time limits the maximum execution time of an ABAP program. 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.
-
If
WHILEloops are used to construct values or fill internal tables, they can probably be expressed more elegantly using conditional iterations withFORin constructor expressions. -
The obsolete addition
VARYcan be used to process a sequence of data objects in the memory.
Example
Replaces all blanks with hyphens in a character-like data object text. Instead
of the loop shown here for demonstration purposes, in a production program the addition ALL
OCCURRENCES of the statement REPLACE
or the predefined function replace with the value 0 for the argument occ should be used for this task.
DATA text TYPE string VALUE `One Two Three`.
WHILE sy-subrc = 0.
REPLACE ` ` IN text WITH `-`.
ENDWHILE.