SAP NetWeaver AS ABAP Release 751, ©Copyright 2017 SAP AG. All rights reserved.

ABAP - Keyword Documentation → ABAP - Reference → Program Flow Logic → Iteration Expressions → FOR - Iteration Expressions → Examples of Iteration Expressions →Creating Values with FOR and REDUCE

The example demonstrates conditional iterations with the operator REDUCE.

Source Code

REPORT demo_reduce_cond_iteration.

CLASS demo DEFINITION.

PUBLIC SECTION.

CLASS-METHODS main.

ENDCLASS.

CLASS demo IMPLEMENTATION.

METHOD main.

DATA(out) = cl_demo_output=>new(

)->next_section( 'Summation'

)->write( REDUCE i( INIT sum = 0

FOR n = 1 UNTIL n > 10

NEXT sum = sum + n )

)->next_section( 'Concatenation without THEN'

)->write( REDUCE string( INIT text = `Count up:`

FOR n = 1 UNTIL n > 10

NEXT text = text && | { n }| )

)->next_section( 'Concatenation with THEN'

)->write( REDUCE string( INIT text = `Count down:`

FOR n = 10 THEN n - 1 WHILE n > 0

NEXT text = text && | { n }| )

)->next_section( 'Non arithmetic expression'

)->write( REDUCE string( INIT text = ``

FOR t = `x` THEN t && `y`

UNTIL strlen( t ) > 10

NEXT text = text && |{ t } | )

)->display( ).

ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

demo=>main( ).

Description

This example demonstrates the following simple iterations, which can be used to construct elementary data objects in a constructor expression using the reduction operator REDUCE and display them directly:

- The first expression constructs a value of the type i for which the total of the iteration variable n is calculated. n is numeric, which means that THEN does not need to be specified. Each iteration step raises the value of n by 1.

- The second expression uses the same iteration as the first, but constructs a text string to which each iteration step appends the character representation of n.

- Like the second expression, the third expression constructs a text string, but uses the addition THEN explicitly to decrement the iteration variable instead of incrementing it.

- Finally, the fourth expression demonstrates that the iteration variable does not have to be numeric. In this case, THEN must be specified explicitly with a suitable expression. Here, a text string is made longer until it has reached a specific size.