Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Program Flow →  Control Structures →  Loops 

WHILE

Short Reference

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 EXIT statement is perfect for completely exiting a loop. Within the statement block, 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 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. 

Continue

ENDWHILE