A nested loop means a loop statement inside another loop statement. That is why nested loops are also called “loop inside loops“. Nested loops in SAP ABAP are a valuable tool when you need to iterate within another loop. They come in handy for processing hierarchical 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.
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:
Similar Reads
SAP Advanced Business Application Programming (ABAP) SAP ABAP stands for Advanced Business Application Programming. SAP ABAP (Advanced Business Application Programming) is a high-level programming language created by the German software company SAP SE. ABAP is primarily used for developing and customizing applications within the SAP ecosystem, which i
6 min read
What is SAP ABAP: A Brief Overview SAP ABAP (Advanced Business Application Programming) is a high-level programming language created by the German software company SAP SE. ABAP is primarily used for developing and customizing applications within the SAP ecosystem, which includes enterprise resource planning (ERP) systems and other bu
8 min read
SAP ABAP | Basic Syntax & Statements The German software company SAP created the high-level programming language, ABAP (Advanced Business Application Programming) primarily, this language serves as a tool for developing applications within the SAP R/3 system. Designed with simplicity and ease of learning in mind, ABAP syntax allows eff
9 min read
SAP ABAP | Understanding Variables What is a Variable?Variable are named data objects that refer to the memory location allocated during the program execution for processing. As the name indicates, users can use refer to the range of values that may be stored inside and the range of actions that can be carried out on the variable.Syn
7 min read
SAP ABAP Keywords The core of many global enterprises, SAP ABAP stands for Advanced Business Application Programming. It is the heart and soul of SAP systems. It gives users the ability to expand and modify SAP apps to satisfy particular business needs. The fundamental building blocks of SAP ABAP are its keywords, wh
5 min read
SAP ABAP | Constants & Literals Explained In the world of SAP ABAP (Advanced Business Application Programming), the use of Constants and Literals is necessary for the effective handling of data. Literals are used to denote specific data types such as numbers, characters, strings, and boolean values.Constants & Literals in SAPWhat are Li
7 min read
SAP ABAP | Data Types Before Understanding the Data type first understand the Data object. Data objects are variables that we declare in the program. It occupies some memory where you can store the data from external sources. Data can be of different types, so data types are responsible for defining the type of data of t
6 min read
Relational operators in SAP ABAP Relational operators in SAP ABAP are symbols or combinations of symbols that compare values and return a Boolean result (either true or false). These operators allow developers to establish relationships between variables, constants, or expressions, facilitating decision-making processes in the prog
6 min read
Operators in SAP ABAP High-level programming languages like SAP ABAP (Advanced Business Application Programming) are used to create apps in the SAP environment. Operators are essential for the execution of many different operations in SAP ABAP, ranging from straightforward arithmetic computations to complex logical analy
7 min read
Loop concept in SAP ABAP