How to use the do Statement in SAP ABAP

The do statement is a basic loop mechanism.
Syntax for the do Statement

The following is the syntax for the do statement.

do [ v1 times ] [ varying f1 from s-c1 next s-c2 [ varying f2 from s2-c1 next s2-c2 ... ] ].

[exit.]

enddo.

where:

* v1 is a variable, literal, or constant.
* s is a field string having the components c1 and c2.
* f1 is a variable. The components of s must be able to be converted to the data type and length of f1.
* … represents any number of complete varying clauses.
* — represents any number of lines of code.

The following points apply:

* do loops can be nested an unlimited number of times.
* exit prevents further loop processing and exits immediately out of the current loop. It does not terminate the program when inside of a do loop. Processing continues at the next executable statement after the enddo.
* You can create an infinite loop by coding do without any additions. In that situation, use exit within the loop to terminate loop processing.
* Modifying the value of v1 within the loop does not affect loop processing.

Within the loop, sy-index contains the current iteration number. For example, the first time through the loop, sy-index will be 1. The second time through, sy-index will be 2, and so on. After enddo, sy-index contains the value it had before entering the loop.

Popularity: 7% [?]

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • Slashdot
  • StumbleUpon
  • Reddit

Tags: , ,

Leave a Reply

You must be logged in to post a comment.