Do while

Páginas: 6 (1336 palabras) Publicado: 3 de junio de 2011
Loops 
In a loop, a statement block is executed several times in succession. There are four kinds of loops in ABAP:
* Unconditional loops using the DO statement.
* Conditional loops using the WHILE statement.
* Loops through internal tables and extract datasets using the LOOP statement.
* Loops through datasets from database tables using the SELECT statement.

UnconditionalLoops
To process a statement block several times unconditionally, use the following control structure:
DO [<n> TIMES] [VARYING <f> FROM <f1> NEXT <f 2>].
  <Statement block>
ENDDO.
If you do not specify any additions, the statement block is repeated until it reaches a termination statement such as EXIT or STOP (see below). The system field SY-INDEX contains thenumber of loop passes, including the current loop pass.
Use the TIMES addition to restrict the number of loop passes to <n>. <n> can be literal or a variable. If <n> is 0 or negative, the system does not process the loop. If you do not use the TIMES option, you must ensure that the loop contains at least one EXIT or STOP statement to avoid endless loops.
You can assign newvalues to a variable <f> in each loop pass by using the VARYING option. You can use the VARYING addition more than once in a DO statement. <f 1 > and <f 2 > are the first two fields of a sequence of fields the same distance apart in memory and with the same type and length. In the first loop pass, <f> takes the value <f 1 >, in the second loop pass, <f 2 >, and soon. If you change the value of the field <f> within the DO loop, the value of the current field <f i > is also changed. You must ensure that there are not more loop passes than fields in the sequence, otherwise a runtime error occurs.
You can nest DO loops and combine them with other loop forms.

Simple example of a DO loop:
DO.
WRITE SY-INDEX.
  IF SY-INDEX = 3.
    EXIT.  ENDIF.
ENDDO.
The output is:
         1          2          3
The loop is processed three times. Here, the processing passes through the loop three times and then leaves it after the EXIT statement.

Example of two nested loops with the TIMES addition:
DO 2 TIMES.
  WRITE SY-INDEX.
  SKIP.
  DO 3 TIMES.
    WRITE SY-INDEX.
  ENDDO.
  SKIP.
ENDDO.
The output is:
         1
        1          2          3
         2
         1          2          3
The outer loop is processed twice. Each time the outer loop is processed, the inner loop is processed three times. Note that the system field SY-INDEX contains the number of loop passes for each loop individually.
Example of the VARYING addition in a DO loop:
DATA: BEGIN OF TEXT,
        WORD1(4) VALUE 'This',        WORD2(4) VALUE 'is',
        WORD3(4) VALUE 'a',
        WORD4(4) VALUE 'loop',
      END OF TEXT.
DATA: STRING1(4), STRING2(4).
DO 4 TIMES VARYING STRING1 FROM TEXT-WORD1 NEXT TEXT-WORD2.
  WRITE STRING1.
  IF STRING1 = 'is'.
    STRING1 = 'was'.
  ENDIF.
ENDDO.
SKIP.
DO 2 TIMES VARYING STRING1 FROM TEXT-WORD1 NEXT TEXT-WORD3
VARYING STRING2 FROM TEXT-WORD2 NEXT TEXT-WORD4.
  WRITE: STRING1,STRING2.
ENDDO.
The output is:
This is a loop
This was a loop
The structure TEXT represents a series of four equidistant fields in memory. Each time the first DO loop is processed, its components are assigned one by one to STRING1. Whenever STRING1 contains ‘is’, its contents are changed to ‘was’. This also changes TEXT-WORD2 to ‘was’. Each time the second DO loop is processed, the componentsof TEXT are passed to STRING1 and to STRING2.

Conditional Loops
To repeat a statement block for as long as a certain condition is true, use the following control structure:
WHILE <condition> [VARY <f> FROM <f1> NEXT <f 2>].
   <statement block>
ENDWHILE.
<condition> can be any logical expression. The statement block between WHILE and...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • ciclos do while, while y for
  • Las Sentencias While Y Do
  • Programacion do-while
  • Ejercicios Do-While
  • Ejercicios Do While
  • Bucle do while
  • Estructura do while
  • estructuras de control de repeticion do while

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS