Get Example source ABAP code based on a different SAP table
• THEN FOR • UNTIL FOR • WHILE FOR
FOR>, Conditional Iteration
ABAP_SYNTAX ... FOR var = rhs $[THEN expr$] UNTIL$|WHILE log_exp> $[let_exp>$] ...>
What does it do? This syntax form of an iteration expression> executes a conditional iteration.
If it is used in a constructor expression with the reduction operator REDUCE>>, the reduction result is created in the iteration steps.
If it is used in a constructor expression with the instance operator NEW>> or with the value operator VALUE>> for internal tables, new table lines are created in the iteration steps and inserted into the tabular result. The parameters and arguments of the iteration expression must be specified as follows:
First, a local helper variable var> must be declared as an iteration variable and assigned a start value rhs> with =>. The same applies to the namespace and visibility of var> as to the helper 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 numeric data type> and is not of type d> or t>, an expression expr> must be 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 numeric data type>, or is of type d> or t>, THEN expr> is optional. If THEN expr> is not specified explicitly, THEN var + 1> is added implicitly or the value of the iteration variable is increased by 1 for every iteration.
Afterwards, a termination condition log_exp> must be specified after UNTIL> or WHILE>. log_exp> is any logical expression> whose operands can be any data objects visible at this position and any calls possible here.
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 before every iteration step. If the result of the logical expression is false, the iteration is terminated. If the result of the logical expression is already false before the first iteration step, no iteration steps are executed.
An optional LET> expression let_exp>> can be specified at the end to define local helper fields. The helper fields are filled in every iteration step and can be used to construct the result. The variables declared in FOR> expressions are local. The local data of all outer FOR> expressions can be used when their values are set. The iteration variable and any helper 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.
Latest notes:
Usually, the expression expr> (after THEN>) and the termination condition log_exp> after UNTIL> or WHILE> depend on the iteration variable var>, but this not a prerequisite . The value of the iteration variable or the termination condition can also be determined in other ways. Status changes, for example, can be queried 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.
Unlike in a LET> expression>, a local field symbol cannot be declared instead of the iteration variable var>. BEGIN_SECTION SAP_INTERNAL_HINT There is not such a thing like a filter condition WHERE> besides UNTIL> and THEN>. As workaround, you can use a conditional expression COND> behind FOR>. END_SECTION SAP_INTERNAL_HINT NON_V5_HINTS
A maximum runtime can be configured using the profile parameters> rdisp/scheduler/prio_low/max_runtime> >, rdisp/scheduler/prio_normal/max_runtime>>, and rdisp/scheduler/prio_high/max_runtime> >. If this runtime is exceeded because the termination condition does not occur in time, the program is terminated by the runtime framework. ABAP_HINT_END
ABAP_EXAMPLE_VX5 The example creates a string from the numbers 0 to 9. ABEXA 01033 ABAP_EXAMPLE_END
ABAP_EXAMPLES_ABEXA Examples of Iteration Expressions > ABAP_EXAMPLE_END