The Wayback Machine - https://web.archive.org/web/20240727121911/https://www.geeksforgeeks.org/sap-abap-do-loop/
Open In App

SAP ABAP | Do Loop

Last Updated : 22 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

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-min

Do loop Flow Diagram

Explanation of Flow Diagram of Do loop

  1. Start: The loop begins execution.
  2. Code Execution: The block of code within the DO loop is executed.
  3. Condition Check: After code execution, the loop checks a specified condition.
  4. 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.


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
Nested Loop in SAP ABAP
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
3 min read
three90RightbarBannerImg