Implementing Conditional Logic

Objective

After completing this lesson, you will be able to Implement conditional logic in an ABAP program.

Implement Conditional Branching

Conditional Branching: Implementation

Keywords IF, ELSEIF, ELSE and ENDIF can be used to implement conditional branching in ABAP code

Conditional branching is a control structure that allows you to make code execution dependent on logical conditions.

The most common conditional branching consists of a pair of keywords, IF and ENDIF. The ABAP code block between IF and ENDIF is only executed if the condition after IF is fulfilled.

You can add more code blocks extending the IF . . ENDIF structure by one by using the keyword ELSE and an arbitrary number of blocks using the keyword ELSEIF. By adding the keyword ELSE, you ensure that exactly one of the ABAP code blocks is always executed. If ELSE is missing, none of the ABAP code blocks may be executed.

Every IF . . ENDIF keywords are mandatory, and every IF must have an ENDIF.

The ABAP code block that is executed is as follows:

  • First, the IF condition is evaluated. If fulfilled, the related ABAP code block is executed, and the program continues after the ENDIF.
  • Only if the IF condition is not fulfilled is the condition after the first ELSEIF evaluated. If fulfilled, the related code block is executed, and the program continues after ENDIF.

  • This is done consecutively for all ELSEIF conditions. If none of the conditions are fulfilled and the structure contains ELSE, the ABAP code block after ELSE is executed. Otherwise, none of the ABAP code blocks are executed.

Hint

As opposed to many other programming languages, ABAP requires a delimiter (.) after each logical condition and even after the keyword ELSE.

Logical Conditions

List of keywords to build logical conditions with three example implementations

Logical conditions are a combination of comparisons, logical operations, expressions, and functions the runtime system evaluates to decide whether the condition is true or false.

The most common use-case for logical conditions is after the keywords IF or ELSEIF in an IF . . . ENDIF structure.

The first example (in the figure above) is a simple comparison: the condition is true if the two data objects, x and y, have the same value.

The second example (in the figure above) is a bit more sophisticated: either the value of x is greater than or equal to y and less than twice the value of y, or it is less than or equal to y and greater than twice the value of y.

The third example (in the figure above) makes use of the arithmetic function abs( ) and logical expression BETWEEN <expression 1> AND <expression 2>. The condition is true if the absolute value of x lies between the absolute value of y and the absolute value of two times y.

For simple value comparisons, you can use operators =, <>, >, <, >=, and <=. You can compare the values of not only data objects but also many other expressions, like the arithmetic expression 2 * y in the example above.

Note

ABAP uses the same symbol (=) for value assignments and value comparisons. The distinction is made based on the position.

Operators and Meanings

OperatorMeaning
AND

Used to join multiple logical expressions to create a new logical expression that is true only when all of the multiple logical expressions are true.

OR

Used to join multiple logical expressions to create a new logical expression that is true if at least one of the logical expressions is true.

NOT

The negation of a logical expression using NOT creates a new logical expression that is false if the logical expression is true and vice versa.

You can use operators AND and OR to combine logical expressions and operator NOT to negate an expression. Without brackets, NOT binds stronger than AND and AND stronger than OR.

ABAP knows some special logical expressions:

  • <data object> IS INITIAL is true if <data object>contains its type-specific value
  • <data object> IS NOT INITIAL is true if <data object> contains a value that is different from the type-specific initial value
  • <data object> BETWEEN <expression 1> AND <expression 2>

Some special ABAP functions are predicate functions.  This means that they are logical conditions themselves. For example, Contains( ) is a function that compares character-like values.

Implement an IF statement

Business Example

Using the IF ... ENDIF keywords users may wish to create and evaluate the logical conditions in an ABAP program and output the results. Create a new application program and use the keywords IF, ELSE, and ENDIF and output the result.

Note

In this exercise XX refers to your number.

Steps

  1. Create a new ABAP class ZCL_S4D100_XX_COND with interface IF_OO_ADT_CLASSRUN.

    1. Create a new ABAP class with the name, ZCL_S4D100_XX_COND and add interface IF_OO_ADT_CLASSRUN.

    2. Assign it to your package and tie it to your transport request.

  2. Activate the application.

    1. Press Activate (Ctrl+F3).

  3. In the METHOD IF_OO_ADT_CLASSRUN~MAIN, define a constant, C_NUMBER0 with an initial value of 0. Use an IF statement to output a message when the value of the constant is zero or not.

    1. Implement the following code in METHOD IF_OO_ADT_CLASSRUN~MAIN.

      Code Snippet
      Copy code
      Switch to dark mode
      1234567891011
      CONSTANTS: c_number0 TYPE i VALUE 0. out->write( '-----------------------------' ). out->write( 'Example 1: IF...ELSE...ENDIF' ). out->write( '-----------------------------' ). IF c_number0 = 0. out->write( 'The value of c_number0 equels zero' ). ELSE. out->write( 'The value of c_number0 equels some numer other than zero' ). ENDIF.
  4. Activate and test the class.

    1. Press Activate (Ctrl+F3).

    2. Press F9 to run the class.

    Practice

Case Statements

Keywords CASE, WHEN, WHEN OTHERS and ENDCASE can also be used to implement conditional branching

A second technique for conditional branching is the CASE . . . WHEN . . . ENDCASE control structure.

Conditional branching with CASE . . . ENDCASE is a special case of the more general branching with IF . . . ENDIF. You can use CASE in situations where the branching depends on the value of a single data object, which you consecutively compare to a set of possible values, using an equal comparison each time.

In the example above, the value of the data object ‘number’ is compared to values 1 and 2. If the value equals 1, then <code_block_1> is executed and if the value equals 2 then <code_block_2> is executed instead. For any other value, the code block after WHEN OTHERS is executed.

Any conditional branching with CASE ... ENDCASE could be implemented within an IF ... ENDIF structure as well. This is illustrated in the example above on the right.

Process Character Strings

Business Example

You have the full name of a customer but you are required to display the first name. Create the ABAP code to split the full name into first name and last name and output the first name to the console.

Note

In this exercise XX refers to your number.

Steps

  1. Create class ZCL_S4D100_XX_COND to ZCL_S4D100_XX_CASE.

    1. Expand CLASS ZLCOAL in the Project Explorer.

    2. Right-click on package ZS4D100_XX and choose NewABAP Class.

    3. In Name enter ZCL_S4D100_XX_CHAR.

    4. In Description: enter Character String.

    5. Choose Add to add the interface IF_OO_ADT_CLASSRUN.

    6. Choose OK.

    7. Choose Next.

    8. Choose your transport request and press Finish.

    9. Press Activate (Ctrl+F3).

  2. Change the code in if_oo_adt_classrun~main to use a CASE statement rather than an IF statement to check if c_number0 is initial or not.

    1. Implement the METHOD IF_OO_ADT_CLASSRUN~MAIN. with the following code:

      Code Snippet
      Copy code
      Switch to dark mode
      1234567
      DATA: lv_full_name TYPE string VALUE 'Stan Wilson', lv_first_name TYPE string, lv_last_name TYPE string. SPLIT lv_full_name AT '' INTO lv_first_name lv_last_name. out->write( |user { lv_first_name }| ).
  3. Activate and test the program.

    1. Press Activate (Ctrl+F3).

    2. Press F9 to run the class.

    Practice

Log in to track your progress & complete quizzes