SAP NetWeaver AS ABAP Release 740, ©Copyright 2014 SAP AG. All rights reserved.
ABAP Keyword Documentation → ABAP − Reference → Program Flow Logic → Control Structures → Loops →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 EXIT statement is perfect 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.
Note
The obsolete addition VARY can be used to process a sequence of data objects in the memory.
Example
Replace all blank characters with hyphens in a character-type data object text. Instead of the loop shown here for demonstartion purposes, in a productive program the ALL OCCURRENCES addition of the statement REPLACE or the built-in replace function 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.