Open In App

Nested Loop in SAP ABAP

Last Updated : 31 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A nested loop means a loop statement inside another loop statement. That is why nested loops are also called “loop inside loops“.  Neste­d loops in SAP ABAP are a valuable tool when you need to iterate within another loop. They come in handy for processing hie­rarchical data or performing complex data operations. Nested loops in SAP ABAP are used in different ways according to the needs of the program. In this article, We will discuss how we will use these nested loops in detail. DO and While loops are used in SAP ABAP for performing Nested operations. we can use one of them or a mix of both to do nested activities in SAP ABAP programs.

SAP-ABAP-Nested-Loop

Nested Loop in SAP ABAP

Syntax of Nested loops in SAP ABAP

DO 3 TIMES.
DO 2 TIMES.
WRITE: / sy-index, sy-index1.
ENDDO.
ENDDO.

In the above syntax of the program, the inner loop is iterating 2 times. In total, the write statements will print 6 times the message. let’s discuss all the ways of using Nested loops in detail:

Example: Do Nested Loop in SAP ABAP

DATA: 
val1 TYPE i,
val2 TYPE i.

val1 = 10.
val2 = 20.

DO 2 TIMES.
val1 = val1 * 2.
WRITE:/ 'val1 = ', val1.
DO 2 TIMES.
val2 = val2 + 5.
WRITE:/ 'val2= :', val2.
ENDDO.
ENDDO.

Output:

val1 = 20
val2 = 25
val2 = 30
val1 = 40
val2 = 35
val2 = 40

In the above example, the first do loop will increase the value val1 twice then inner loop will run 2 times and increase the val2 by adding 5 into it. in this way the above output will be generated.

Example: While Nested Loop in SAP ABAP

DATA: inner TYPE i , outer TYPE i.

inner =1.
outer = 1.

WHILE outer < =2.
WRITE: / 'outer = ', outer.
WHILE inner <= 3.
WRITE: / 'inner = ', inner.
inner = inner + 1.
ENDWHILE
outer = outer +1.
inner=1.
ENDWHILE.

Output:

outer = 1
inner = 1
inner = 2
inner = 3
outer = 2
inner = 1
inner = 2
inner = 3

In the above example, the outer while loop will run 2 times and in every outer loop the inner while loop will run 3 times.

Example: While nested loop with Do in SAP ABAP

DATA: inner TYPE i , outer TYPE i.  
inner =1.
outer = 1.

WHILE outer < =2.
WRITE: / 'outer = ', outer.
DO 2 TIMES.
WRITE: / 'inner = ', inner. i
inner = inner + 1.
ENDDO.
outer = outer +1.
inner=1.
ENDWHILE.

Output:

outer = 1
inner = 1
inner = 2
outer = 2
inner = 1
inner = 2

In the above example, the outer while loop will run 2 times and in every outer loop the inner Do loop will run 2 times.

Example: Do Nested Loop with While in SAP ABAP

DATA: inner TYPE i , outer TYPE i.  
inner =1.
outer = 1.

DO 2 TIMES.
WRITE: / 'outer = ', outer.
WHILE inner< =2.
WRITE: / 'inner = ', inner.
inner = inner + 1.
ENDWHILE.
outer = outer +1.
inner=1.
ENDDO.

Output:

outer = 1
inner = 1
inner = 2
outer = 2
inner = 1
inner = 2

In the above example, the outer Do loop will run 2 times and in every outer loop the inner While loop will run 2 times.

For more Reference Please visit:


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads