The Wayback Machine - https://web.archive.org/web/20240727121915/https://www.geeksforgeeks.org/nested-loop-in-sap-abap/
Open In App

Nested Loop in SAP ABAP

Last Updated : 31 Oct, 2023
Comments
Improve
Suggest changes
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:


Previous Article
Next Article

Similar Reads

SAP ABAP: A complete Guide to Events in ABAP Report
SAP created the high-level programming language known as SAP ABAP (Advanced Business Application Programming) to be used for creating business applications. Reports are an essential tool for extracting and presenting data in SAP development. To manage the execution flow and improve the functionality of their systems, developers must comprehend the
5 min read
SAP S/4 HANA Vs SAP HANA | Difference between SAP S/4 HANA , SAP HANA
SAP S/4HANA is the most recent ERP suite, with advanced architecture allowing for real-time processing. SAP HANA is an in-memory database that supports S/4HANA, whereas SAP ECC is S/4HANA's traditional ERP predecessor. The distinctions are in the streamlined architecture and enhanced capabilities of S/4HANA. What is SAP S/4 HANA?SAP, which stands f
3 min read
SAP HANA Vs SAP ECC | Difference between SAP HANA and SAP ECC
SAP HANA is an in-memory database that supports S/4HANA, whereas SAP ECC is S/4HANA's traditional ERP predecessor. The distinctions are in the streamlined architecture and enhanced capabilities of S/4HANA. What is SAP HANA?SAP HANA stands for High-performance analytic appliance, which is faster than any other database management system that we use
2 min read
SAP MM Vs SAP SRM | Difference Between SAP MM and SAP SRM
SAP MM helps companies optimize their material and logistics management procedures, guaranteeing effective and economical handling of items. SAP SRM stands for Supplier Relationship Management and is an efficient tool that helps companies optimize procurement operations and helps to improve relationships with suppliers and other business vendors. W
2 min read
SAP EWM Vs SAP WM | Difference Between SAP EWM and SAP WM
SAP EWM is a version created by enhancing the SAP Warehouse Management (SAP WM) solution, one of the world’s most popular warehouse management applications, with new features. What is SAP EWM?The SAP EWM solution was created to organize and manage goods storage in warehouses, distribution centers, and logistical centers. Its primary goal is to impr
3 min read
Diffrence Between SAP and SAP ABAP(Advanced Business Application Programming)
In this article, we are going to learn the difference between an SAP and SAP ABAP. SAP ABAP is created by SAP which is a domain-specific language, it is 4th generation programming language. It is also called ABAP/4(Fourth Generation Language” or 4GL). What is SAP?SAP stands for Systems, Applications, and Products in Data Processing. It’s a German m
2 min read
Difference Between SAP ERP and SAP ABAP
In this article, we are going to learn the difference between an SAP ERP and SAP ABAP. SAP ABAP is created by SAP which is a domain-specific language, it is 4th generation programming language. It is also called ABAP/4(Fourth Generation Language” or 4GL). SAP Enterprise Resource Planning (ERP) systems are essential for simplifying and maximizing a
2 min read
SAP ABAP | Loop Control
Introduction to Loop Control in SAP ABAPIn SAP ABAP programming, loop control is an esse­ntial concept that allows you to execute­ a block of code multiple times. This is e­specially useful when proce­ssing data in an efficient manner. Loops help automate repetitive­ tasks and handle large datasets effectively. In ABAP, there are various types of l
4 min read
SAP ABAP | While Loop
A fundamental control structure in SAP ABAP is the while loop. The while loop in SAP ABAP can be used to iterate a part of code while the given condition remains true. This construct the while loop facilitates iterative running a block of code continues to execute until the specified condition within that same loop becomes false. Programmers, throu
3 min read
SAP ABAP | Do Loop
DO loop in SAP ABAP is an unconditional loop that executes a block of code several times without providing a specific condition. It is a fundamental construct that facilitates the iterative execution of a code block until a specified condition is met. Programmers, through the incorporation of do loops, can automate processes; manipulate data, and c
5 min read
three90RightbarBannerImg