FOR expressions let you map, filter and build internal tables inside constructor expressions without a separate result table and APPEND loop.
Learning outcome: Use table and conditional iterations with VALUE, WHERE and INDEX INTO, and know when a normal LOOP is clearer.
1. Replace LOOP + APPEND
DATA(lt_ids) = VALUE ty_ids(
FOR ls_order IN lt_orders
( ls_order-id ) ).2. Map rows to a new structure
DATA(lt_summary) = VALUE ty_summaries(
FOR ls_order IN lt_orders
( id = ls_order-id
amount = ls_order-amount
category = COND #( WHEN ls_order-amount >= 1000
THEN 'HIGH' ELSE 'NORMAL' ) ) ).3. Filter during iteration
CODE SCREEN 1 — MAP + FILTER
DATA(lt_open) = VALUE ty_orders(
FOR ls_order IN lt_orders
WHERE ( status = 'O' )
( ls_order ) ).4. Capture the loop index
DATA(lt_ranked) = VALUE ty_ranked_tab(
FOR ls_order IN lt_orders INDEX INTO lv_index
( rank = lv_index id = ls_order-id ) ).5. Conditional iteration
CODE SCREEN 2 — UNTIL
DATA(lt_numbers) = VALUE ty_numbers(
FOR i = 1 THEN i + 1 UNTIL i > 5
( i ) ).Conditional iterations also support WHILE. Keep the termination condition obvious.
6. Nested FOR
DATA(lt_pairs) = VALUE ty_pairs(
FOR ls_header IN lt_headers
FOR ls_item IN lt_items
WHERE ( header_id = ls_header-id )
( header_id = ls_header-id item_id = ls_item-id ) ).7. FOR or LOOP?
- Use VALUE ... FOR to construct one clear result.
- Use LOOP for side effects, early exit or complex debugging.
- Helper variables belong only to the expression.
- Modern syntax does not automatically improve performance.
8. Practice exercises
- Create a table containing only order IDs.
- Keep only open orders.
- Add a sequential rank with INDEX INTO.
- Generate 2, 4, 6, 8 and 10 using conditional FOR.
9. Interview quick checks
Where can FOR be used?
Inside constructor expressions such as VALUE, NEW and REDUCE.
What are its main variants?
Table iteration with IN and conditional iteration with UNTIL or WHILE.
Official SAP references
Educational examples; verify syntax availability for your ABAP release.

0 Comments