ERP Database - The Unofficial ERP Knowledge Base

Facebook Twitter del.icio.us Digg it
ERP Database contains a huge collection of articles related to ERP System and Software. Many of the articles are specifically related to SAP. All these ERP/SAP articles are freely available to everyone.

If you would like to submit an article or share any document related to ERP or any specific ERP software. Please mail it to support@erpdb.info. Please make sure that the documents are not copyrighted.
Social Bookmarks:

DO : ABAP Keyword a day

Print This Post Email This Post Written by admin on Jan 18th, 2008 | Filed under: ABAP Keywords

DO
Variants

1. DO.
2. DO n TIMES.
Variant 1
DO.
Addition

… VARYING f FROM f1 NEXT f2
Effect
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 a DO loop, the value of SY-INDEX belonging to the outer DO loop is restored.
Example

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.
Note
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.
Addition 1
… VARYING f FROM f1 NEXT f2
Effect
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.
Example

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 THEN WORD-THREE
VARYING LETTER2 FROM WORD-TWO THEN WORD-FOUR.
WRITE: LETTER1, LETTER2.
IF LETTER2 = '!'.
EXIT.
ENDIF.
ENDDO.

The resulting output is the character string

“E x a m p l e !”.
Note
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.
Variant 2
DO n TIMES.
Addition

… VARYING f FROM f1 NEXT f2 (similar to variant 1)
Effect
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.
Example

DATA COUNT TYPE I.
DO 10 TIMES.
ADD SY-INDEX TO COUNT.
ENDDO.

The field COUNT now contains 55 (1+2+…+10).
Related WHILE , LOOP
Note
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.

Share

If you like this post, you may as well like these too:

  1. AT : ABAP Keyword a day AT Events in lists- AT LINE-SELECTION.- AT USER-COMMAND.- AT PFn.Events on selection screens- AT SELECTION-SCREEN.Control break with extracts- AT NEW f.- AT END OF f.- AT FIRST.- AT LAST.- AT...
  2. GET: ABAP Keyword a day GET: Event keyword for defining event blocks for reporting events. GET <node> [FIELDS <fi> <f 2>...]. GET node LATE [FIELDS f1 f2 ...]. Only occurs in executable programs. When the...
  3. ASSIGN: ABAP Keyword a day ASSIGN Variants:1. ASSIGN f TO .2. ASSIGN (f) TO .3. ASSIGN TABLE FIELD (f) TO .4. ASSIGN LOCAL COPY OF MAIN TABLE FIELD (f) TO .5. ASSIGN COMPONENT idx OF...
  4. FORM: ABAP Keyword a day FORM: Defines a subroutine. Syntax FORM [USING ... [VALUE(][)] [TYPE |LIKE ]„. ] [CHANGING... [VALUE(][)] [TYPE |LIKE ]„. ]. Introduces a subroutine. The USING and CHANGING additions define the subroutine’s...
  5. FETCH : ABAP Keyword a day FETCH Basic form FETCH NEXT CURSOR c target. Effect Uses the cursor c to read the next line or lines from the dataset of a database table determined by OPEN...
  6. EXPORT : ABAP Keyword a day EXPORT 1 2 3 4 *Export data - EXPORT obj1 ... objn TO MEMORY. - EXPORT obj1 ... objn TO DATABASE dbtab(ar) ID key. - EXPORT obj1 ... objn TO...
  7. DESCRIBE : ABAP Keyword a day DESCRIBE Return attributes of a field - DESCRIBE FIELD f. Return attributes of an internal table - DESCRIBE TABLE itab. Determine distance between two fields - DESCRIBE DISTANCE BETWEEN f1...
  8. DELETE : ABAP Keyword a day DELETE Delete from a database table - DELETE FROM dbtab WHERE condition. - DELETE FROM (dbtabname) WHERE condition. - DELETE dbtab. - DELETE *dbtab. - DELETE (dbtabname) … . -...
  9. DATA : ABAP Keyword a day DATA Variants1. DATA f.2. DATA f(len).3. DATA: BEGIN OF rec,…END OF rec.4. DATA: BEGIN OF itab OCCURS n,…END OF itab.5. DATA: BEGIN OF COMMON PART c,…END OF COMMON PART.EffectDefines global...
  10. CONTINUE: ABAP Keyword a day CONTINUE Basic formCONTINUE.EffectWithin loop structures like * DO … ENDDO* WHILE … ENDWHILE* LOOP … ENDLOOP* SELECT … ENDSELECTCONTINUE terminates the current loop pass, returns the processing to the beginning...
  11. CONCATENATE : ABAP Keyword a day CONCATENATE Basic form CONCATENATE f1 … fn INTO g. Addition … SEPARATED BY h Effect Places the fields f1 to fn after g . With the fields fi (1 <=...
  12. COMPUTE: ABAP Keyword a day COMPUTEBasic formCOMPUTE n = arithexp.EffectEvaluates the arithmetic expression arithexp and places the result in the field n .Allows use of the four basic calculation types + , – , *...
  13. CNT : ABAP Keyword a day CNT Basic form… CNT(h) …EffectCNT(h) is not a statement, but a field which is automatically created and filled by the system if f is a sub-field of an extract dataset...
  14. CASE: ABAP Keyword a day CASE Basic formCASE f. Effect Case distinction.Depending on the current contents of a field, this statement executes one of several alternative processing branches. The field whose contents determine how the...
  15. GET PARAMETER: ABAP Keyword a day GET PARAMETER Gets an SPA/GPA parameters. Syntax GET PARAMETER ID <pid> FIELD <field_name>. Writes the value of the SPA/GPA parameter <pid> from the user-specific SAP memory into the variable <f>....



Leave a Reply