SAP ABAP Loops and Decision-Making Statements

FREE Online Courses: Elevate Your Skills, Zero Cost Attached - Enroll 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?

  • While writing a program, at times, we need to execute one statement more than once.
  • One way of doing this is, by writing the statement more than once in the code itself.
  • However, this is a tedious task – what if you want to execute a statement 100 times?!
  • To avoid this inefficiency and redundancy in code, we have loops.

Loops is SAP ABAP

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 –

LoopDescription
LOOP ATRepeats statements within the LOOP statement according to the range of values of a test variable.
WHILEChecks a condition and then loops through the statements if the test condition evaluates to true.
DOLoops 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:

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?

  • The code you write usually is not a straightforward run, executing line-by-line.
  • At times, you need to jump from one line to another based on a condition, or a ‘decision’
  • For e.g. if you ask the user if they want option ‘A’ or option ‘B’, the user enters their choice as input and you continue the execution based on their choice.
  • These are nothing but decision-making statements.

Decision Making Statements in SAP ABAP

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 –

StatementDescription
IFExecutes statements within if the test condition evaluates to true, otherwise ignores the statements enclosed.
IF.. ELSEExecutes 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.. ELSEWhen 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’.
CASEWill 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.

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *