Example to apply DO statement in ABAP/4

You will apply this statement when you want to loop data with non-condition loops.

Example 1: Simple Loop

DO.
WRITE:/ sy-index.
IF sy-index = 4.
    EXIT.
ENDIF.
ENDDO.

The output is


1
2
3
4

Example 2: Loops with n times

DO 4 TIMES.
WRITE:/ sy-index.
ENDDO.

The output is same as example 1.

Example 3: Nested Loops


DO.
WRITE:/ sy-index.
IF sy-index = 4.
    EXIT.
ENDIF.
DO 2 TIMES.
WRITE sy-index.
ENDDO.

ENDDO.

The output is 


1 1 2
2 1 2
3 1 2
4

Example 4: Loops with VARYING addition


DATA: BEGIN OF wa_text,
fld1(6) VALUE ’I’,
fld2(6) VALUE ’AM’,
fld3(6) VALUE ’A’,
fld4(6) VALUE ’ABAPER’,
END OF wa_text,
lv_text(6).
DO 4 TIMES VARYING lv_text FROM wa_text-fld1 NEXT wa_text-fld2.
WRITE:/ lv_text.
ENDDO.

The output is


I AM A ABAPER

Post new comment