Open In App

SAP ABAP | While Loop

Last Updated : 17 Oct, 2023
Improve
Improve
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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads