Get Started with ABAP Programming on SAP BTP

Using Control Structures in ABAP

Objectives
After completing this lesson, you will be able to:

After completing this lesson, you will be able to:

  • Implement conditional branching
  • Implement Iterations
  • Handle Exceptions

Implementing Conditional Branching

A conditional branching is a control structure that allows you to make the execution of code dependent on logical conditions.

Let's look at the different techniques of conditional branching.

Try It Out: Conditional Branching

  1. Like in the first exercise of this course, create a new global class that implements interface IF_OO_ADT_CLASSRUN.
  2. Copy the following code snippet to the implementation part of method if_oo_adt_classrun~main( ):
    Code snippet
    
    * Declarations
    **********************************************************************
    
        CONSTANTS c_number TYPE i VALUE 0.
    *    CONSTANTS c_number TYPE i VALUE 1.
    *    CONSTANTS c_number TYPE i VALUE 2.
    *    CONSTANTS c_number TYPE i VALUE -1.
    *    CONSTANTS c_number TYPE i VALUE -2.
    
    * Example 1: Simple IF ... ENDIF.
    **********************************************************************
    
        out->write(  `--------------------------------` ).
        out->write(  `Example 1: Simple IF ... ENDIF.` ).
        out->write(  `-------------------------------` ).
    
        IF c_number = 0.
          out->write( `The value of C_NUMBER equals zero`   ).
        ELSE.
          out->write( `The value of C_NUMBER is NOT zero`   ).
        ENDIF.
    
    * Example 2: Optional Branches ELSEIF and ELSE
    **********************************************************************
    
        out->write(  `--------------------------------------------` ).
        out->write(  `Example 2: Optional Branches ELSEIF and ELSE` ).
        out->write(  `--------------------------------------------` ).
    
        IF c_number = 0.
          out->write( `The value of C_NUMBER equals zero`            ).
        ELSEIF c_number > 0.
          out->write( `The value of C_NUMBER is greater than zero`   ).
        ELSE.
          out->write( `The value of C_NUMBER is less than zero`      ).
        ENDIF.
    
    * Example 3: CASE ... ENDCASE
    **********************************************************************
    
        out->write(  `---------------------------` ).
        out->write(  `Example 3: CASE ... ENDCASE` ).
        out->write(  `---------------------------` ).
    
        CASE c_number.
          WHEN 0.
            out->write( `The value of C_NUMBER equals zero`             ).
          WHEN 1.
            out->write( `The value of C_NUMBER equals one`              ).
          WHEN 2.
            out->write( `The value of C_NUMBER equals two`              ).
          WHEN OTHERS.
            out->write( `The value of C_NUMBER equals non of the above` ).
        ENDCASE. 
    
    Copy code
  3. Press CTRL + F3 to activate the class and F9 to execute it as a console app.
  4. Analyze the console output. Play around with the source code to get familiar with the concepts; Uncomment different declarations of constant c_number with different values to see, which branches of the code get executed.

Implementing Iterations

Iterations are control structures that define a block of code which is executed several times.

Watch this video to learn how.

Try It Out: Iterations

  1. Like in the first exercise of this course, create a new global class that implements interface IF_OO_ADT_CLASSRUN.
  2. Copy the following code snippet to the implementation part of method if_oo_adt_classrun~main( ):
    Code snippet
    
    * Declarations
    **********************************************************************
    
        CONSTANTS c_number TYPE i VALUE 3.
    *    CONSTANTS c_number TYPE i VALUE 5.
    *    CONSTANTS c_number TYPE i VALUE 10.
    
        DATA number TYPE i.
    
    * Example 1: DO ... ENDDO with TIMES
    **********************************************************************
    
        out->write(  `----------------------------------` ).
        out->write(  `Example 1: DO ... ENDDO with TIMES` ).
        out->write(  `----------------------------------` ).
    
        DO c_number TIMES.
          out->write(  `Hello World` ).
        ENDDO.
    
    * Example 2: DO ... ENDDO with Abort Condition
    **********************************************************************
    
        out->write(  `-------------------------------` ).
        out->write(  `Example 2: With Abort Condition` ).
        out->write(  `-------------------------------` ).
    
        number = c_number * c_number.
    
        " count backwards from number to c_number.
        DO.
    
          out->write( |{ sy-index }: Value of number: {  number }| ).
          number = number - 1.
    
          "abort condition
          IF number <= c_number.
            EXIT.
          ENDIF.
    
        ENDDO.
    
    Copy code
  3. Press CTRL + F3 to activate the class and F9 to execute it as a console app.
  4. Analyze the console output. Play around with the source code to get familiar with the concepts; Uncomment different declarations of constant c_number to see how the different values affect the result.

Handling Exceptions

Exceptions

What are exceptions? Let's take a look.

Exception Handling

Now that you have learned about exceptions, let's see how you can handle them.

Try It Out: Exception Handling

  1. Like in the first exercise of this course, create a new global class that implements interface IF_OO_ADT_CLASSRUN.
  2. Copy the following code snippet to the implementation part of method if_oo_adt_classrun~main( ):
    Code snippet
    
    * Declarations
    **********************************************************************
        DATA result TYPE i.
    
        DATA numbers TYPE TABLE OF i.
    
    * Preparation
    **********************************************************************
    
        APPEND 123 TO numbers.
    
    * Example 1: Conversion Error (no Number)
    **********************************************************************
    
        CONSTANTS c_text TYPE string VALUE 'ABC'.
    *    CONSTANTS c_text TYPE string VALUE '123'.
    
        out->write(  `---------------------------` ).
        out->write(  `Example 1: Conversion Error` ).
        out->write(  `---------------------------` ).
    
        TRY.
            result = c_text.
            out->write( |Converted content is { result }|  ).
          CATCH cx_sy_conversion_no_number.
            out->write( |Error: { c_text } is not a number!| ).
        ENDTRY.
    
    * Example 2: Division by Zero
    **********************************************************************
    
        CONSTANTS c_number TYPE i VALUE 0.
    *    CONSTANTS c_number TYPE i VALUE 7.
    
        out->write(  `---------------------------` ).
        out->write(  `Example 2: Division by Zero` ).
        out->write(  `---------------------------` ).
    
        TRY.
            result = 100 / c_number.
            out->write( |100 divided by { c_number } equals { result }| ).
          CATCH cx_sy_zerodivide.
            out->write(  `Error: Division by zero is not defined!` ).
        ENDTRY.
    
    * Example 3: Itab Error (Line Not Found)
    **********************************************************************
    
        CONSTANTS c_index TYPE i VALUE 2.
    *    CONSTANTS c_index TYPE i VALUE 1.
    
        out->write(  `-------------------------` ).
        out->write(  `Example 3: Line Not Found` ).
        out->write(  `-------------------------` ).
    
        TRY.
            result = numbers[ c_index ].
            out->write( |Content of row { c_index } equals { result }| ).
          CATCH cx_sy_itab_line_not_found.
            out->write(  `Error: Itab has less than { c_index } rows!` ).
        ENDTRY.
    
    
    * Example 4: Combination of Different Exceptions
    **********************************************************************
    *    CONSTANTS c_char TYPE c LENGTH 1 VALUE 'X'.
    *    CONSTANTS c_char TYPE c length 1 value '0'.
        CONSTANTS c_char TYPE c LENGTH 1 VALUE '1'.
    *    CONSTANTS c_char TYPE c length 1 value '2'.
    
        out->write(  `----------------------` ).
        out->write(  `Example 4: Combination` ).
        out->write(  `----------------------` ).
    
        TRY.
            result = numbers[ 2 / c_char ].
            out->write( |Result: { result } | ).
          CATCH cx_sy_zerodivide.
            out->write( `Error: Division by zero is not defined`  ).
          CATCH cx_sy_conversion_no_number.
            out->write( |Error: { c_char } is not a number! | ).
          CATCH cx_sy_itab_line_not_found.
            out->write( |Error: Itab contains less than { 2 / c_char } rows| ).
        ENDTRY.
    
    Copy code
  3. Press CTRL + F3 to activate the class and F9 to execute it as a console app.
  4. Analyze the console output. Play around with the source code to get familiar with the concept:
    • Comment the exception handling to make the system raise a runtime error.
    • Change the values of constants c_number, c_text, c_index, and c_charin a way that no exceptions are raised.

Save progress to your learning plan by logging in or creating an account