Learn Selection IF, CASE, Repetition in ABAP Control Structures

Tutorial Objectives
1. Learn how to use branches for decision making: IF..ELSEIF..ELSE and CASE..WHEN..ENDCASE.
2. Learn how to use loops with conditions: DO..ENDDO and WHILE..ENDWHILE.
Prerequisites
1. SAP System Access: SAP GUI, ECC.
2. Authorization: Developer role, TCODE: SE38.

Today you will learn about control structures in ABAP, which is the programming constructs that will help developers to control the flow of a program.

They include branches for decision making statements and loops that determine how the code executes based on specific conditions. In this tutorial, we’ll dive into the various control structures in ABAP, their syntax, and practical examples.

1. Branches: ABAP Decision Making Statement

IF, ELSEIF, ELSE

The first control structure that I want to talk about is for making decision based on your certain condition using the IF, ELSEIF, ELSE statement. You can also chain multiple conditions using ELSEIF and provide a fallback with ELSE.

Basic Syntax:

IF <condition one>. 
   "Do this statement
ELSEIF <condition two>.
   "Do this statement if another condition is true
ELSEIF <condition three>.   
   "Do this statement if another condition is true
ELSE.
   "Do this statement if none of the above conditions are true
ENDIF.

Example:

In the example below, we determine the grade of a score based on specific conditions:


DATA: lv_score TYPE i,
      lv_grade TYPE c.

lv_score = 80. "assign value into your variable

IF lv_score >= 85. "condition 1
  lv_grade = 'A'.
ELSEIF lv_score >= 75 AND lv_score < 85. "condition 2
  lv_grade = 'B'.
ELSEIF lv_score >= 65 AND lv_score < 75. "condition 3
  lv_grade = 'C'.
ELSE. "the fallback
  lv_grade = 'D'.
ENDIF.

WRITE:/ 'Your score is:', lv_score.
WRITE:/ 'Your grade is:', lv_grade.

Case, When, When OTHERS.

The CASE statement is an alternative to the conditional IF statement but is designed for simpler scenarios. It is limited to comparing a single variable and cannot handle complex conditions like those in IF statements, where multiple conditions can be combined using operators such as < or >.

Basic Syntax:

CASE variable_name. 
  WHEN value1. 
    do statement_block1.
  WHEN value2.
    do statement_block2.
  WHEN OTHERS. 
    do statement_block3.
ENDCASE. 

Example:

If we want to determine grades for a score, as in our previous IF statement example, the CASE statement falls short. This is because it can only compare a single variable. Trying to force it for such use cases would result in cumbersome and inefficient code, as shown below.

CASE lv_score.
  WHEN '85' or '86' or '87' . "etc until 100
  WHEN '75' or '76' or '77'. "etc until 84
  WHEN OTHERS.
ENDCASE.

So use the CASE statement to compare single value. For conditions requiring complex operators, it’s better to use the IF statement. The CASE statement is ideal for scenarios like determining the output of a single grade value, as demonstrated below.

CASE lv_grade.
  WHEN 'A'.
    WRITE:/ 'Excellent'.
  WHEN 'B'.
    WRITE:/ 'Good Job'.
  WHEN 'C'.
    WRITE: / 'Fair enough!'.
  WHEN 'D' or 'E'.
    WRITE: / 'Better luck next time!'.
ENDCASE.

Conditional Expressions Come To Rescue

Even though the CASE statement has some limitations, ABAP provides a solution with conditional expressions. These allow you to handle such logic more efficiently by using the CASE statement in an in-line declaration. This approach not only simplifies your code but also makes it more readable and concise, addressing the shortcomings of traditional CASE statements.

Basic Syntax:

result = COND data_type(
    WHEN <condition_1> THEN <value_1>
    WHEN <condition_2) THEN <value_2>
    ELSE <value_3>
).

Example:

With conditional expressions, you can achieve complex conditions similar to those in an IF statement, all while keeping your code clean and concise. Check out the example below, pretty cool right?

lv_grade = COND text1( WHEN lv_score >= 85 THEN 'A'
                        WHEN lv_score >= 75 AND lv_score < 85 THEN 'B'
                        WHEN lv_score >= 65 AND lv_score < 75 THEN 'C'
                        ELSE 'D' ).

2. LOOPS: DO And WHILE

In ABAP, loops are powerful constructs that allow you to execute a block of code multiple times. Two common loop types in ABAP are DO and WHILE. These loops will add flexibility in iterating through code depending on your requirements.

Now let’s see how these two statement work.

The DO Statement

Keep in mind that when using the DO statement, the repetition doesn’t have any condition or unconditional loop so you can add your own logic inside the loop.

The Basic Syntax For DO Statement

DO [x TIMES] ...   

[statement_block].

ENDDO.

The Example of Basic Usage

The example below demonstrates a basic usage of the DO statement, where the WRITE statement is used to display the current iteration number (sy-index). To prevent an infinite loop, an EXIT condition is included to stop the loop once the iteration reaches 10. Without this handler, the loop would run indefinitely.

DO.
  WRITE sy-index.
  IF sy-index = 10.
    EXIT.
  ENDIF.
ENDDO.

Here’s the result

Other alternative is by using x TIMES syntax to achieve the same result as shown below.

DO 10 TIMES.
  WRITE sy-index.
ENDDO.

You can control the flow within a loop using the CONTINUE statement to skip a single iteration and move to the next, or the EXIT statement to terminate the loop entirely. Below is an example demonstrating both approaches.

DO 10 TIMES.
  IF sy-index = 5.
    CONTINUE.  "The result: 1 2 3 4 6 7 8 9 10
  ENDIF.
  "IF sy-index = 5.
  "   EXIT.    "The result: 1 2 3 4
  "ENDIF.
  WRITE sy-index.
ENDDO.

Here’s the result adding the CONTINUE statement, as you can see, once the iteration reaches 5, that specific loop is skipped, and the program immediately proceeds to the next iteration, which is 6.

By using the EXIT statement, the iteration will stop entirely.

The WHILE statement

Now for a conditional LOOP we will be using the WHILE statement, so the statement block will be executed as long as the condition logic is TRUE.

WHILE <condition_logic>.
   <statement_block>.
ENDWHILE.

Here’s the example.

WHILE sy-index <= 10.
  WRITE sy-index.
ENDWHILE.

You can also use stopper such as EXIT and CONTINUE just like in the DO example.

Summary

As you can see, control structures in ABAP form the backbone of decision making and iterative logic. Whether you’re using IF, CASE, loops, or conditional expressions, these constructs make your programs dynamic and flexible. By mastering these, you can write efficient and readable ABAP code tailored to various scenarios.

More resource: ABAP control structures

Share this tutorial with Fellow Geeks!

Other ABAP Tutorials

Leave a Reply

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