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

SAP ABAP | While Loop

Last Updated : 17 Oct, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

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, through the incorporation of while loops, can automate processes; manipulate data, and control program flow based on dynamic conditions.

Syntax for While Loop in SAP ABAP:

WHILE condition

” Statements to be executed

ENDWHILE.

Flow Diagram for While loop in SAP ABAP:

Flow-Diagram-of-SAP-ABAP-while-loop

Flow Diagram of SAP ABAP | While Loop

Examples of While Loop in SAP ABAP:

Example 1: Simple while loop to print numbers from 1 to user defined limit

DATA: counter TYPE i VALUE 1.
WRITE 'Enter the limit:'.
READ VALUE counter.
WHILE counter <= limit.
WRITE: / counter.
counter = counter + 1.
ENDWHILE.


Input:

Enter the limit: 7

Output:

1
2
3
4
5
6
7

Explanation:

First, the code prompts the user for a limit input. Subsequently, it enters a while loop; in each iteration of this loop until reaching the limit defined by the user, it prints out and updates (increments) its counter variable: initially set to 1.

Example 2: Find factorial of a user-provided number:

DATA: factorial TYPE i VALUE 1,
i TYPE i VALUE 1.
WRITE 'Enter the number:'.
READ VALUE i.
WHILE i <= number.
factorial = factorial * i.
i = i + 1.
ENDWHILE.
WRITE: / 'Factorial of', number, 'is', factorial.


Input:

Enter the number: 5

Output:

Factorial of 5 is 120

Explanation:

The user is prompted by this code to input a number for factorial calculation. A while loop within it calculates the entered number’s factorial. The factorial variable initializes at 1, and with each iteration of the loop from one to the user-provided value, every value multiplies into it.

Example 3: While loop to concatenate a user-provided string a certain number of times:

DATA: input_string TYPE string, repetitions TYPE i,  counter TYPE i VALUE 1,
output_string TYPE string VALUE space.
WRITE 'Enter a string:'.
READ VALUE input_string.
WRITE 'Enter the number of repetitions:'.
READ VALUE repetitions.
WHILE counter <= repetitions.
CONCATENATE output_string input_string INTO output_string.
counter = counter + 1.
ENDWHILE.
WRITE: / 'Concatenated string:', output_string.


Input:

Enter a string: ABAP
Enter the number of repetitions: 3

Output:

Concatenated string: ABAPABAPABAP

Explanation:

The user is prompted by this code to input a string and specify the number of times it should be repeated; subsequently, a while loop concatenates the input string as directed. Each iteration involves concatenating the initially empty output_string with the input_string until or unless the counter aligns with specified repetitions.

For more reference Please visit: SAP ABAP Loop Control


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