Open In App

SAP ABAP | Decision Control Statements

Last Updated : 01 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

SAP ABAP, or Advanced Business Application Programming, provides a robust set of tools for developing business applications within the SAP environment. Decision control statements are a crucial aspect of programming logic, allowing developers to make choices and guide the flow of a program based on certain conditions.

Decision Control statement have one or more block of statement that are to be evaluated by the program and is executed only when certain conditions are met.

SAP-ABAP--Decision-Control-Statements

Flow of Decision Control Statements in SAP ABAP

Before diving into the specifics of decision control statements, let’s visualize their flow. Decision control statements create branches in the code, directing the program’s execution path based on the evaluation of conditions. Here’s a simplified flow diagram:

Start
|
|-- [Condition A]
| |
| |-- Code Block A1
| |
| |-- Code Block A2
|
|-- [Condition B]
| |
| |-- Code Block B1
| |
| |-- Code Block B2
|
|-- [Default]
|
|-- Default Code Block

This diagram represents the typical structure of decision control statements, illustrating how the program follows different paths based on evaluated conditions.

Types of Decision Control Statements in SAP ABAP

There are several types of decision control statements in SAP ABAP, each serving specific programming needs:

1. IF Statement

If the condition is true, evaluates it and then executes a piece of code.

Syntax:

IF condition.
" Code to execute if the condition is true.
ENDIF.

Example:

DATA(age) = 25.

IF age < 30.
WRITE: 'You are young.'.
ENDIF.

2. IF-ELSE Statement:

Adds an alternative block of code to execute if the condition in the IF statement is false.

Example:

IF condition.
" Code to execute if the condition is true.
ELSE.
" If the condition is not true i.e false, code will executed.
ENDIF.

Example:

DATA(grade) = 'B'.

IF grade = 'A'.
WRITE: 'Excellent!'.
ELSE.
WRITE: 'Good effort!'.
ENDIF.

3. IF-ELSEIF-ELSE Statement

Evaluates multiple conditions in sequence and executes the block of code corresponding to the first true condition.

Example:

IF condition1.
" Code will execute if condition1 is true.
ELSEIF condition2.
" Code will execute if condition2 is true.
ELSE.
" If any of the criteria is not met, code will run.
ENDIF.

Example:

DATA(marks) = 75.

IF marks >= 90.
WRITE: 'A Grade.'.
ELSEIF marks >= 80.
WRITE: 'B Grade.'.
ELSE.
WRITE: 'C Grade.'.
ENDIF.

4. Nested IF Statements

Allows nesting of IF statements inside one another for more complex condition evaluations.

Example:

IF condition1.
IF condition2.
" If both the condition are met, code must run.
ENDIF.
ENDIF.

Example:

DATA(age) = 25.
DATA(income) = 50000.

IF age < 30.
IF income > 40000.
WRITE: 'Young and earning well.'.
ENDIF.
ENDIF.

5. CASE Statement

Provides a concise way to evaluate a variable against multiple values.

Example:

CASE variable.
WHEN value1.
" If value1 is equal to variable, code will run.
WHEN value2.
" If value2 is equal to variable, code will run.
WHEN OTHERS.
" Code will execute if none of the specified values match.
ENDCASE.

Example:

DATA(day) = 'Monday'.

CASE day.
WHEN 'Monday'.
WRITE: 'The start of the week.'.
WHEN 'Friday'.
WRITE: 'TGIF!'.
WHEN OTHERS.
WRITE: 'Another day.'.
ENDCASE.

Conclusion

In conclusion, effective programming requires a solid understanding of SAP ABAP’s Decision Control Statements. With the help of these statements, developers may design dynamic, adaptable, and efficient business solutions in the SAP environment by utilising CASE structures or IF-ELSE conditions.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads