Implementing Conditional Logic

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

After completing this lesson, you will be able to:

  • Implement conditional logic in an ABAP program

Implement Conditional Branching

Conditional Branching: Implementation

Conditional branching is a control structure that allows you to make the execution of code 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 . . ENIF 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 always exactly one of the ABAP code blocks is executed. If ELSE is missing, then it is possible that none of the ABAP code blocks is 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 it is fulfilled, the related ABAP code block is executed, and the program continues after the ENDIF.
  • Only if the IF condition is not fulfilled, the condition after the first ELSEIF is evaluated. If it is 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 of the logical conditions and even after keyword ELSE.

Logical Conditions

Logical conditions are a combination of comparisons, logical operations, expressions, and functions that 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 and 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 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 not only compare the values of data objects, but the values of many other expressions like the arithmetic expressions 2 * y in the example above.

Note
ABAP uses the same symbol (=) for value assignments for 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 which 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. Contains( ) is a function that compares character-like values and line_exists ( ) performs an existence check for a row in an internal table.

Implement an IF statement in a Program

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, ENDIF and output the result.

Prerequisites

Steps

  1. Create a new ABAP class.

    1. Create a new ABAP class with the name, ZCL_S4D100_01_COND.

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

  2. Activate the application.

    1. Activate the application, ZCL_S4D100_01_COND.

  3. Define the Interface (IF_OO_ADT_CLASSRUN) in the PUBLIC section.

    1. Implement the METHOD IF_OO_ADT_CLASSRUN~MAIN.

    2. Define a constant C_NUMBER0 of type I value zero.

    3. Use IF…ELSE…ENDIF conditional branching and output the result.

    4. Use the OUT->WRITE statement to output the result.

    5. Activate the program.

  4. Test the program and remove the output.

    1. Test the program by pressing (F9).

    2. Choose the Clear Console button to remove the output.

    Practice

Case Statements

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 compared to a set of possible values, using an equals comparison each time.

In the example above, the value of data object ‘number’ is compared to value 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.

Note
In contradiction to what you might be used to from other programming languages, sy-index starts with value 1 during the first iteration.

Implement a CASE statement in a Program

Business Example

Using the CASE . . ENDCASE keywords, users may want to create and evaluate the logical conditions in an ABAP program and output the results. Create a new application program and use the keywords CASE . . ENDCASE and output the result.

Prerequisites

Steps

  1. Copy the class and add it to your package.

    1. Copy the CLASS ZCL_S4D100_##_COND using the name, ZCL_S4D100_##_CASE.

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

  2. Activate the application.

    1. Activate the application, ZCL_S4D100_##_CASE.

  3. Define the Interface (IF_OO_ADT_CLASSRUN) in the PUBLIC section.

    1. Implement the METHOD IF_OO_ADT_CLASSRUN~MAIN.

    2. Define a constant C_NUMBER0 of type I value zero.

    3. Use CASE…ENDCASE conditional branching and output the result.

    4. Use the OUT->WRITE statement to output the result.

    5. Activate the program.

  4. Test the program and remove the output.

    1. Test the program by pressing (F9).

    2. Choose the Clear Console button to remove the output.

    Practice

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