Site icon DataFlair

SAP ABAP Loops and Decision-Making Statements

sap abap loops and decision making statements

FREE Online Courses: Click, Learn, Succeed, Start Now!

In this tutorial, we are going to learn about SAP ABAP Loops & Decision-Making Statements – what they are, why they are used and how to use them. Let’s have you cosied up in using iterations and conditions in ABAP.

So let us start!!!

What is a Loop?

A loop, therefore, is an iterative statement that helps to execute the same line or set of lines of code multiple times, based on a few conditions.

Loops in ABAP

ABAP has the following types of loop –

Loop Description
LOOP AT Repeats statements within the LOOP statement according to the range of values of a test variable.
WHILE Checks a condition and then loops through the statements if the test condition evaluates to true.
DO Loops through statements and then checks whether the condition provided evaluates to true. Opposite to ‘while’ condition in a way.

Here is an example of using Loop At in ABAP –

REPORT ZR_SS_DATAFLAIR_SAMPLE_001.

DATA: df TYPE I.

df = 0.

LOOP AT df.

   Write: / 'Data Flair Loop Number ', df.
   df = df + 2.

ENDLOOP.

This is an example of using While Loop in ABAP –

REPORT ZR_SS_DATAFLAIR_SAMPLE_001.

DATA: df TYPE I.

df = 0.

WHILE df <= 10.

   Write: / 'Data Flair Loop Number ', df.
   df = df + 2.

ENDWHILE.

Here is an example of using Do in ABAP –

REPORT ZR_SS_DATAFLAIR_SAMPLE_001.

DATA: df TYPE I.

df = 0.

DO 10 TIMES.

   Write: / 'Data Flair Loop Number ', df.
   df = df + 2.

ENDWHILE.

Below is the Output of all 3 above programs:

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Output

Data Flair Loop Number 0
Data Flair Loop Number 2
Data Flair Loop Number 4
Data Flair Loop Number 6
Data Flair Loop Number 8
Data Flair Loop Number 10

What are Decision-Making Statements?

Decision-making statements, therefore, are statements that check a condition and execute only the statements that correspond with the condition given.

Decision-Making Statements in ABAP

ABAP has the following types of decision-making statements –

Statement Description
IF Executes statements within if the test condition evaluates to true, otherwise ignores the statements enclosed.
IF.. ELSE Executes statements within ‘if’ if the test condition of ‘if’ evaluates to true, if it evaluates to false, it executes the statements within ‘else’.
IF.. ELSEIF.. ELSE When the test condition within ‘if’ is true, it evaluates those statements. If false, it checks conditions within ‘elseif’ and executes statements within if true. If neither of these evaluate to true, it will execute statements under ‘else’.
CASE Will check the condition of a test variable via equality with each case, and execute statements under the case with exactly matching values as test variables.

Here is an example of a decision-making statement in ABAP – (Nested If.. Else)

REPORT ZR_SS_DATAFLAIR_SAMPLE_001.

Data:
      DataFlair1 TYPE I,
      DataFlair2 TYPE I,
      DataFlair3 TYPE I.

DataFlair1 = 35.
DataFlair2 = 53.
DataFlair3 = 88.

IF DataFlair1 = 35.
  IF DataFlair2 = 53.
     IF DataFlair1 + DataFlair2 = DataFlair3.
       Write: 'The answer is ', DataFlair3.
     ELSE.
       Write: 'The answer is NOT ', DataFlair3.
     ENDIF.
  ENDIF.
ENDIF.

Output

The answer is 88

Here is another example of a decision-making statement in ABAP – (Case statement)

REPORT ZR_SS_DATAFLAIR_SAMPLE_001.

DATA:
      DataFlair1 TYPE I,
      DataFlair2 TYPE I.

DataFlair1 = 25.
DataFlair2 = 50.

CASE DataFlair2.

WHEN 25.
   Write 'This is DataFlair1, not DataFlair2'.

WHEN 50.
   Write 'This is DataFlair2, yay!'.

WHEN OTHERS.
   Write 'This is invalid, try again!'.

ENDCASE.

Output

This is DataFlair2, yay!

Summary

Thus, in this tutorial, we learnt the various types of looping, or iterative statements and decision-making, or conditional statements.

These statements are of great help when we want to execute lines in a program based on certain conditions that need to be fulfilled.

Exit mobile version