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 operatorVALUE
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 valuelhs
with=
must be assigned to this auxiliary variable. The same applies to the namespace and visibility ofvar
as applies to the auxiliary fields declared in aLET
expression. The syntax of the declaration is exactly the same as in aLET
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 typed
ort
, then an expressionexpr
is specified afterTHEN
. The result of this expression can be converted into a data type ofvar
. The expression is calculated for every iteration and its result is assigned to the iteration variablevar
. This is a general expression position.
- If the iteration variable
var
has a numerical data type, or the variable is of typed
ort
,THEN expr
is optional. IfTHEN 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 afterUNTIL
orWHILE
.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 expressionlog_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 expressionlog_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
expressionlet_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
(afterTHEN
) and the termination conditionlog_exp
(afterUNTIL
orWHILE
) depend on the iteration variablevar
. 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 afterWHILE
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
andWHILE
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 variablevar
.