Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Program Flow Logic →  Iteration Expressions →  FOR - Iteration Expressions 

FOR - Conditional Iteration

Other versions: 7.31 | 7.40 | 7.54

Syntax


 ... FOR var = lhs [THEN expr] UNTIL|WHILE log_exp [let_exp] ...

Effect

This syntax form of an iteration expression executes a conditional iteration.

  • When this syntax is used in a constructor expression with the reduction operator REDUCE, the reduction result is created in the iteration steps.
  • If this syntax is used in a constructor expression with the instancing operator NEW or with the value operator VALUE for internal tables, then new table rows are created in the iteration steps und added to the tabular result.

The parameters and arguments of the iteration expression must be specified as follows:

  • First a local auxiliary variable var must be declared as an iteration variable and a start value lhs with = must be assigned to this auxiliary variable. The same applies to the namespace and visibility of varas applies to the auxiliary fields declared in a LET expression. The syntax of the declaration is exactly the same as in a LET expression and it follows the rules that apply here.
  • The next position depends on the data type of the iteration variable var:
  • If the iteration variable var does not have a numerical data type and is not of type d or t, then an expression expr is specified after THEN. The result of this expression can be converted into a data type of var. The expression is calculated for every iteration and its result is assigned to the iteration variable var. This is a general expression position.
  • If the iteration variable var has a numerical data type, or the variable is of type d or t, THEN expr is optional. If THEN expr is not explicitly specified, THEN var + 1 is implicitly added or the value of the iteration variable is increased by 1 for every iteration.
  • Next a termination condition log_exp must be specified after UNTIL or WHILE. log_exp is any logical expression whose operands can be all visible data objects and any possible calls at this place.
  • If the termination condition is specified after UNTIL, the logical expression log_exp is evaluated after every iteration step. If the result of the logical expression is true, the iteration is ended. At least one iteration step is executed.
  • If the termination condition is specified after WHILE, the logical expression log_exp is evaluated after every iteration step. If the result of the logical expression is false, the iteration is ended. If the result of the logical expression is false even before the first iteration step, no iteration steps are executed.
  • A LET expression let_exp can be specified (optional) at the end to define local auxiliary fields. The auxiliary fields are used in every iteration step and can be used to construct the result.

The variables declared in FOR expressions are local. The local data from all outer FOR expressions can be used when their values are defined. The iteration variable and auxiliary variables can be used after the FOR expression; either in additional subexpressions or to construct the result.

The system field sy-index is not set by a FOR expression.


Notes

  • Usually the expression expr (after THEN) and the termination condition log_exp (after UNTIL or WHILE) depend on the iteration variable var. However this not mandatory. The value of the iteration variable or the termination condition can also be determined in other ways. For example, status changes can be retrieved using method calls.

  • Usually a termination condition after UNTIL is preferable to a termination condition after WHILE in all cases where the termination condition does not have to be checked before the first iteration step.

  • In many cases, iteration expressions for conditional iterations can replace DO and WHILE loops, which construct values and internal tables.

  • Multiple sequential FOR expressions with different variants (including the tabular iterations) can be specified in a constructor expression. These expressions then work in the same way as nested loops.

  • The profile parameter rdisp/max_wprun_time limits the maximum execution time of an ABAP program. If this runtime is exceeded because the termination condition does not occur in time, the program is ended by the runtime environment.

  • Unlike in a LET expression expression, a local field symbol cannot be declared instead of the iteration variable var.

Examples

See Examples of Iteration Expressions.