Start of Content Area

Function documentation Loop Constructs  Locate the document in its SAP Library structure

Use

DO Loop

      Statements that are between DO and ENDDO can be repeated as often as required.

      The loop is interrupted by the EXIT statement.

      All values of a variable are iterated with the FOREACH <Variable>. statement. The characteristic values concerned are those available in the transaction data of the current data object (therefore not necessarily all the master data values that are defined for this characteristic). This loop construct is completed with the ENDFOR statement.{0>Die Zahl der Schleifendurchläufe kann durch eine Konstante oder eine Variable vom Typ I gesteuert werden: DO <n> TIMES.<}77{>»The number of loop passes can also be controlled by a constant or a variable of type I: DO <n> TIMES.

 

 

«<0}

FOREACH Loop

      All values of a variable are iterated with the FOREACH <Variable>. statement. The characteristic values concerned are those available in the transaction data of the current data object (therefore not necessarily all the master data values that are defined for this characteristic). This loop construct is completed with the ENDFOR statement.

      The characteristic values are processed in ascending order. If you need combinations of characteristic values, you can implement the statement in the form FOREACH <Variable1, Variable2>.. This construct gets the available combinations of characteristic values of the current data object.

Note

Notes on runtime behavior: Before entering the FOREACH loop, all the characteristic values available in the data object are collected and sorted, then these characteristic values in the FOREACH loop are delivered one after the other. Of a new value is created in the loop and written to the transaction data, this is only taken into account in the next FOREACH loop.

The FOREACH construct requires a lot of processing time and should be implemented as little as possible. Consider carefully whether you could use a construct of the type FOREACH <Variable1, Variable2>. instead of two nested loops as this would improve behavior at runtime.

      Further variants of the FOREACH loop:

       All the characteristic values that are available in the reference data are iterated with FOREACH <Variable> IN REFDATA.

       All characteristic values of the active planning filter are iterated with FOREACH <Variable> IN SELECTION.

       All characteristic values of a global Var variable are iterated with FOREACH <Variable> IN VARIABLE Var.

Example

The following example shows a bad program:

DATA COUNTRY TYPE 0BPS_CNTRY.

DATA PRODUCT TYPE 0BPS_PRODU.

DATA FISCPER TYPE 0FISCPER.

FOREACH COUNTRY.

FOREACH PRODUCT.

FOREACH FISCPER.

{COUNTRY, PRODUCT, FISCPER} = {COUNTRY, PRODUCT, FISCPER} * 2.

ENDFOR.

ENDFOR.

ENDFOR.

The following example shows a better program: Here it is ensure that new values are only created when they are different from null. A lot of processing time is saved because the number of values that could appear in the PRODUCT and FISCPER variables does not need to be refreshed. Such a refresh occurs only when both of the loops FOREACH PRODUCT. and FOREACH FISCPER. are executed again. If null values are produced by a formula, the system does not write this to the internal buffer. These null values are therefore not visible to subsequent planning functions or manual planning. On the other hand, data not available is also assigned the value 0. Null values therefore do not need to be created explicitly.

DATA COUNTRY TYPE 0BPS_CNTRY.

DATA PRODUCT TYPE 0BPS_PRODU.

DATA FISCPER TYPE 0FISCPER.

FOREACH COUNTRY.

FOREACH PRODUCT.

FOREACH FISCPER.

IF {COUNTRY, PRODUCT, FISCPER} <> 0.

{COUNTRY, PRODUCT, FISCPER} = {COUNTRY, PRODUCT, FISCPER} * 2.

ENDIF.

ENDFOR.

ENDFOR.

ENDFOR.

Finally, the following example shows the recommended way of programming:

DATA COUNTRY TYPE 0BPS_CNTRY.

DATA PRODUCT TYPE 0BPS_PRODU.

DATA FISCPER TYPE 0FISCPER.

FOREACH COUNTRY, PRODUCT, FISCPER.

IF NOT {COUNTRY, PRODUCT, FISCPER} IS INITIAL.

{COUNTRY, PRODUCT, FISCPER} = {COUNTRY, PRODUCT, FISCPER} * 2.

ENDIF.

ENDFOR.

 

End of Content Area