SAP WHILE ABAP Statements

Get Example source ABAP code based on a different SAP table
  


Standard SAP Help for WHILE

WHILE

Short Reference
• WHILE ABAP Statement


ABAP Syntax WHILE log_exp

[statement_block]
ENDWHILE.

What does it do? 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.



Latest notes:The obsolete addition VARY
can be used to process a sequence of data objects in the memory.



Example ABAP Coding
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.

Return to menu