Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Obsolete Language Elements →  Obsolete Program Flow →  Obsolete Control Structures 

DO - VARYING

Short Reference

Other versions: 7.31 | 7.40 | 7.54

Obsolete Syntax

DO ... VARYING dobj FROM dobj1 NEXT dobj2 [RANGE range]
      [VARYING ...].
  [statement_block]
ENDDO.

Effect

The addition VARYING assigns a new value to a variable dobj for each pass of a DO loop. It can be used more than once in a DO statement.

dobj1 and dobj2 are the first two data objects in a string of data objects that are the same distance apart in the memory. The data types of dobj, dobj1, and dobj2 must be flat and compatible with each other. dobj1 and dobj2 must be parts of a flat data object. These parts must be either structure components of the same structure or subareas of the same data object specified using offsets/lengths.

In the first pass of the loop, the content of the data object dobj1 is assigned to dobj, and in the second loop, the content of the data object dobj2 is assigned. In the subsequent loops, dobj is assigned the content of the data object that is the same distance in the memory from the previously assigned data object, as dobj2 is from dobj1. There is no type conversion.

If the processing block is exited correctly using ENDDO, CHECK, or EXIT, the content of the variable dobj at the end of the loop pass is assigned to the previously assigned data object dobj1 or dobj2 without conversion. If it is exited using another statement, such as RETURN or RAISE EXCEPTION, no conversion takes place.

The addition RANGE defines the memory area that can be processed using the addition VARYING. After RANGE, an elementary data object range of type c, n, or x, or a structure can be specified. The memory area of range must include the ranges of dobj1 and dobj2. In deep structures, the deep components are exceptions to the permitted area. The DO loop must be ended before non-permitted memory areas are accessed, that is, areas outside of range or their deep components. Otherwise an unhandleable exception is raised.

The addition RANGE can be omitted only if a static check determines that dobj1 and dobj2 are components from the same structure. The permitted memory area is then determined from the smallest substructure that contains dobj1 and dobj2.


Notes

  • The restrictions above do not apply to the data objects dobj, dobj1, and dobj2.

  • The addition RANGE cannot always be omitted. This means that the permitted memory area extends from dobj1 to the limit of the current data area of the ABAP program and there is a risk of unintentional memory overwrites.

Example

In the first DO loop, subareas of the data object text are edited using offset/length access. In Unicode programs, the addition RANGE must be specified here. In the second DO loop, the program accesses the components of the data object text. In this case, it is not necessary to specify RANGE. The third DO loop shows how the functions of the second loop can be programmed using the statement ASSIGN INCREMENT.

DATA: BEGIN OF text, 
        word1 TYPE c LENGTH 4 VALUE 'AAAA', 
        word2 TYPE c LENGTH 4 VALUE 'BBBB', 
        word3 TYPE c LENGTH 4 VALUE 'CCCC', 
        word4 TYPE c LENGTH 4 VALUE 'DDDD', 
      END OF text. 

DATA: word  TYPE c LENGTH 4, 
      char1 TYPE c LENGTH 1, 
      char2 TYPE c LENGTH 1, 
      leng TYPE i. 

FIELD-SYMBOLS <word> LIKE text-word1. 
DATA inc TYPE i. 

DESCRIBE FIELD text LENGTH leng IN CHARACTER MODE. 
leng = leng / 2. 

cl_demo_output=>begin_section( `First Loop` ). 

DO leng TIMES VARYING char1 FROM text(1) 
                            NEXT text+2(1) RANGE text 
              VARYING char2 FROM text+1(1) 
                            NEXT text+3(1) RANGE text. 
  cl_demo_output=>write( |{ char1 } { char2 }| ). 
  char1 = 'x'. 
  char2 = 'y'. 
ENDDO. 

cl_demo_output=>next_section( `Second Loop` ). 

DO 4 TIMES VARYING word FROM text-word1 NEXT text-word2. 
  cl_demo_output=>write( word ). 
ENDDO. 

cl_demo_output=>next_section( `Third Loop` ). 

DO. 
  inc = sy-index  - 1. 
  ASSIGN text-word1 INCREMENT inc TO <word> RANGE text. 
  IF sy-subrc = 0. 
    cl_demo_output=>write( <word> ). 
  ELSE. 
    EXIT. 
  ENDIF. 
ENDDO. 

cl_demo_output=>display( ). 

Exceptions


Non-Catchable Exceptions

  • Cause: Invalid access to deep components within the area specified by the RANGE-addition.
    Runtime Error: DO_WHILE_VARY_ILLEGAL_ACCESS
  • Cause: Access to data outside the range specified by the RANGE addition.
    Runtime Error: DO_WHILE_VARY_NOT_IN_RANGE