DO.
1. ... VARYING
f FROM f1 NEXT f2
2. ... VARYING f FROM f1 NEXT f2 RANGE f3
Repeats the processing enclosed by the
DO and ENDDO
statements until the loop is terminated by EXIT,
STOP or REJECT.
You can use the CONTINUE
statement to end the current loop pass prematurely and continue with the next loop pass.
The
system field SY-INDEX counts the number of loop passes,
starting from 1. You can nest DO loops. When the processing
leaves an inner
DO loop, the value of SY-INDEX belonging to the outer DO loop is restored.
DO.
WRITE: / 'SY-INDEX - Begin:', (3) SY-INDEX.
IF SY-INDEX = 10.
EXIT.
ENDIF.
WRITE: 'End:', (3) SY-INDEX.
ENDDO.
This
DO loop outputs 9 lines of the form
"SY-INDEX
- Begin: n End: n
".
Here, n stands for the numbers 1 to 9.
The last line displayed is
"SY-INDEX - Begin: 10>".
On the 10th pass, the loop is terminated.
The danger with this statement is programming endless loops. Therefore, you must ensure that the loop
contains at least one
EXIT, STOP or REJECT statement which is executed at least once.
... VARYING f FROM f1 NEXT f2
This addition is useful if you have a series of fields of the same type and the same distance from each
other.
f is a variable which you define in a DATA
statement. On each loop pass, f contains a new value.
The field f1 after "FROM"
specifies the first value of the variable f, while the
field f2 after "NEXT"
specifies the value to be assigned to the variable f in
the second pass. For each subsequent pass, the variable f
contains the next value in the sequence determined by the distance between the fields
f1 and f2 in
memory.
The fields f1 and f2
should be type-compatible and convertible to f.
If
the value of f changes during the loop pass, the new value
is then placed in the appropriate field fn assigned to
f (transfer type: pass by value and result). If the loop
pass terminates because of a dialog message, the new value is not passed back if
f changes.
The addition ...
VARYING f FROM f1 NEXT f2 can be used several times in a DO statement.
In some cases, the syntax rules that apply to Unicode programs are different than those for non-Unicode programs. See Other Changes Relating to Unicode
DATA: BEGIN OF WORD,
ONE VALUE 'E',
TWO VALUE 'x',
THREE
VALUE 'a',
FOUR VALUE 'm',
FIVE VALUE
'p',
SIX VALUE 'l',
SEVEN
VALUE 'e',
EIGHT VALUE '!',
END
OF WORD,
LETTER1, LETTER2.
DO VARYING LETTER1 FROM WORD-ONE
NEXT WORD-THREE
VARYING LETTER2 FROM WORD-TWO NEXT WORD-FOUR.
WRITE:
LETTER1, LETTER2.
IF LETTER2 = '!'.
EXIT.
ENDIF.
ENDDO.
The resulting output is the character string
"E x a m p l e !".
When using this addition, ensure that the DO loop terminates
at the "right" time, in order to avoid assigning meaningless values that happen to be in memory after this sequence of fields. This could result in a runtime error.
... VARYING f FROM f1 NEXT f2 RANGE f3
This addition has a similar effect to ...VARYING f FROM f1 NEXT f2.
It also ensures that the system only accesses data within the field f3.
In some cases, the syntax rules that apply to Unicode programs are different than those for non-Unicode programs. See Other Changes Relating to Unicode
DO n TIMES.
1. ... VARYING f FROM f1 NEXT f2 (similar to variant
1)
2. ... VARYING f FROM f1 NEXT f2 RANGE f3(similar to variant 1)
Repeats the processing enclosed by the
DO and ENDDO
statements n times. If n changes within the loop, this has no effect on loop passes.
DATA COUNT TYPE I.
DO 10 TIMES.
ADD SY-INDEX TO COUNT.
ENDDO.
The field COUNT now contains 55 (1+2+...+10).
Performance:
The runtime required to pass once through an empty
DO loop is about 11 msn (standardized microseconds).
For 100 loop passes, about 230 msn would be needed.
If possible, use a WHILE loop
instead of a
DO / EXIT construction because this improves the performance slightly and is clearer.
Non-Catchable Exceptions
Loops