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 control program flow based on dynamic conditions. In this article, we'll delve into the syntax, flow diagram, and practical examples of the DO
loop in SAP ABAP.
Syntax of Do Loop
The syntax of the DO
loop in SAP ABAP is straightforward and resembles the structure of other programming languages.
DO .
<Code to be executed repeatedly>
ENDDO.
The DO
loop creates a block of code enclosed between DO
and ENDDO
. The loop will continue to execute the code within this block until a specified condition is met.
Flow Diagram of the DO
Loop
Before we explore examples, let's visualize the flow of the DO
loop:
Do loop Flow Diagram
Explanation of Flow Diagram of Do loop
- Start: The loop begins execution.
- Code Execution: The block of code within the
DO
loop is executed. - Condition Check: After code execution, the loop checks a specified condition.
- Continue or Exit: If the condition is true, the loop returns to step 2 for another iteration. If false, the loop exits, and program control moves to the next statement after
ENDDO
.
Now, let's dive into practical examples to illustrate the usage of the DO
loop.
Examples of Do Loop
Example 1: Simple Loop Iteration
DATA: counter TYPE i VALUE 1.
DO.
WRITE: / 'Iteration:', counter.
counter = counter + 1.
IF counter > 5.
EXIT. " Exit the loop when counter exceeds 5
ENDIF.
ENDDO.
Output:
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Explanation
This example demonstrates a basic usage of the DO
loop. First, the counter variable is initialized by 1. Subsequently, it enters a do loop, in each iteration of this loop until counter is greater than 5 , it prints out and updates (increments) its counter variable: It iterates five times, printing the current iteration number. The loop exits when the counter exceeds 5.
Example 2: Looping Through an Internal Table
DATA: names TYPE TABLE OF string,
index TYPE i VALUE 1.
names = VALUE #( ( 'Alice' ) ( 'Bob' ) ( 'Charlie' ) ).
DO.
WRITE: / 'Name:', names[ index ].
index = index + 1.
IF index > lines( names ).
EXIT. " Exit when all names are processed
ENDIF.
ENDDO.
Output:
Name: 'Alice'
Name: 'Bob'
Name: 'Charlie'
Explanation:
In this example, names
is an internal table of strings containing the names 'Alice', 'Bob', and 'Charlie'. The DO
loop iterates over the elements of the internal table. In each iteration, it writes the current name to the output. The loop continues until all names in the internal table are processed. The condition IF index > lines( names ).
checks whether the current index index
exceeds the number of lines in the internal table (lines(names )
). So the output is 'Alice', 'Bob', 'Charlie'.
Example 3: Dynamic Loop Termination
DATA: limit TYPE i VALUE 3,
counter TYPE i VALUE 1.
DO.
WRITE: / 'Current Counter:', counter.
counter = counter + 1.
IF counter > limit.
EXIT. " Exit when the counter exceeds the dynamically set limit
ENDIF.
ENDDO.
Output
Current Counter: 1
Current Counter: 2
Current Counter: 3
Explanation:
In this Example, limit variable
is set to 3. Do loop iterates over the counter variable, writing its current value to the output in each iteration. The loop continues until the counter variable (counter
) exceeds the dynamically set limit (limit
).The EXIT
statement is used to exit the loop when the counter exceeds the limit. So the output is 1,2,3.
Here, the loop iterates until the counter exceeds a dynamically set limit. This showcases the flexibility of the DO
loop
Example 4: Infinite Loop with User Input
DATA: input TYPE i.
DO.
WRITE: / 'Enter a number (0 to exit):'.
READ input.
IF input = 0.
EXIT. " Exit when the user enters 0
ENDIF.
WRITE: / 'You entered:', input.
ENDDO.
Input :
10
5
0
Output:
Enter a number (0 to exit):
You entered: 10
Enter a number (0 to exit):
You entered: 10
Enter a number (0 to exit):
Explanation:
This example creates an interactive loop, continuously prompting the user for input until they enter 0 to exit. It reads the user's input into the variable input
. It checks if the entered value is 0. If so, it exits the loop using the EXIT
statement. If the entered value is not 0, it displays the message indicating the entered number.
Conclusion
DO
loop in SAP ABAP is a versatile construct for handling repetitive tasks efficiently. Its simple syntax and adaptability make it a powerful tool for developers working within the SAP environment. As you explore more in ABAP programming, mastering the nuances of the DO
loop will undoubtedly enhance your ability to create robust and efficient SAP applications.
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 SAP What are
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