Learn SAP Free
Back to Dashboard
ABAP Programming

IF ELSE Statement in ABAP with Examples | Complete Beginner Guide

Daksh | Last Updated: August 4, 2026 | 9 min read

IF ELSE Statement in ABAP

When learning SAP development, you will quickly reach a point where your programs need to make decisions. Whether determining if an employee qualifies for a bonus, checking if a customer is eligible for a discount, or blocking a transaction due to failed validations, decision-making logic is a fundamental requirement.

In ABAP, IF ELSE is the primary statement used to control program flow based on conditions. Although the syntax is straightforward, you will find it in almost every report, transaction, database validation, and user enhancement you build.


Understanding Conditional Logic

To understand how conditions work, think of a standard banking transaction.

When a customer attempts to withdraw cash, the system checks a single condition: Is the account balance greater than or equal to the requested withdrawal amount?

  • If yes: The bank processes the withdrawal.
  • If no: The system blocks the transaction and displays an insufficient funds message.

Programs assess variables in the same way, executing specific branches of code depending on whether a condition resolves to true or false.


Basic Syntax of IF ELSE

The basic structure of an IF statement is:

IF condition.
  " Statements executed when condition is true
ELSE.
  " Statements executed when condition is false
ENDIF.

Every IF block must end with the ENDIF keyword. The ELSE block is optional and is only used if you want to run alternative statements when the condition fails.


Your First IF ELSE Example

Let’s look at a simple standalone program that evaluates a test score:

REPORT z_if_else_demo.

DATA lv_marks TYPE i.
lv_marks = 75.

IF lv_marks >= 35.
  WRITE 'Pass'.
ELSE.
  WRITE 'Fail'.
ENDIF.

Output:

Pass

Since the variable lv_marks holds the value 75 (which is greater than or equal to 35), the condition resolves to true, and the program prints ‘Pass’.


Practical Examples

1. Evaluating Salary Bonuses

Conditional validation is heavily used in HR and payroll applications. This program determines bonus eligibility based on salary thresholds:

REPORT z_salary_check.

DATA lv_salary TYPE i.
lv_salary = 60000.

IF lv_salary > 50000.
  WRITE 'Bonus Eligible'.
ELSE.
  WRITE 'Bonus Not Eligible'.
ENDIF.

Output:

Bonus Eligible

2. Validating User Input with PARAMETERS

To make reports interactive, we can capture inputs directly from selection screens using the PARAMETERS statement and evaluate them:

REPORT z_age_check.

PARAMETERS p_age TYPE i.

IF p_age >= 18.
  WRITE 'Eligible to Proceed'.
ELSE.
  WRITE 'Not Eligible (Underage)'.
ENDIF.

When you execute this program, SAP displays an input field. If you enter 22, the output is:

Eligible to Proceed

Handling Multiple Outcomes (ELSEIF)

In many business scenarios, you need to check more than two possibilities. You can chain multiple conditions using the ELSEIF statement:

REPORT z_grade_calculator.

DATA lv_score TYPE i.
lv_score = 82.

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

Output:

Grade: A

The system checks each condition sequentially from top to bottom. As soon as one condition evaluates to true, SAP executes its statements and jumps directly past ENDIF.


Standard Comparison Operators in ABAP

ABAP supports two types of comparison operators: standard symbols and character-based operators. You can use either format in your programs:

OperationSymbolic OperatorCharacter-based Operator
Equal To=EQ
Not Equal To<>NE
Greater Than>GT
Less Than<LT
Greater Than or Equal To>=GE
Less Than or Equal To<=LE

ABAP-Specific String Comparison Operators

In addition to standard numeric comparisons, ABAP provides powerful character string comparison operators that are unique to the language:

OperatorFull MeaningDescription & Example
COContains OnlyTrue if field 1 contains only characters from field 2. IF '1234' CO '0123456789' -> True.
CNContains Not onlyTrue if field 1 contains characters not present in field 2.
CAContains AnyTrue if field 1 contains at least one character from field 2. IF 'ABAP' CA 'A' -> True.
NAContains No AnyTrue if field 1 contains no characters from field 2.
CSContains StringTrue if field 1 contains the string in field 2 (case-insensitive). IF 'SAP ABAP' CS 'abap' -> True.
NSNo StringTrue if field 1 does not contain the string in field 2.
CPContains PatternTrue if field 1 matches pattern with wildcards (* or +). IF 'MAT100' CP 'MAT*' -> True.
NPNo PatternTrue if field 1 does not match wildcard pattern.

Practical String Checking Code Example:

REPORT z_string_operators_demo.

DATA gv_input TYPE string VALUE 'PO-902184'.

* Check if input starts with PO- using CP (Contains Pattern)
IF gv_input CP 'PO-*'.
  WRITE: / 'Valid Purchase Order Format!'.
ENDIF.

* Check if input contains digits only using CO (Contains Only)
DATA gv_numeric_code TYPE string VALUE '902184'.
IF gv_numeric_code CO '0123456789'.
  WRITE: / 'Code is strictly numeric.'.
ENDIF.

Checking State & Initialization: IS INITIAL, IS BOUND, IS ASSIGNED

In SAP enterprise development, checking whether variables, objects, or field symbols contain values is critical for preventing runtime crashes:

1. IS INITIAL / IS NOT INITIAL

Checks if a variable or internal table contains its default initial state (blank for strings, 0 for integers, empty for tables):

DATA: gt_customers TYPE TABLE OF kna1,
      gv_city      TYPE string.

IF gt_customers IS INITIAL.
  WRITE: / 'No customer records loaded into memory.'.
ENDIF.

IF gv_city IS NOT INITIAL.
  WRITE: / 'City parameter supplied:', gv_city.
ENDIF.

2. IS BOUND / IS NOT BOUND

Checks if an Object Reference variable points to a valid instance:

DATA: go_alv TYPE REF TO cl_gui_alv_grid.

IF go_alv IS BOUND.
  go_alv->refresh_table_display( ).
ELSE.
  WRITE: / 'ALV Grid object not instantiated!'.
ENDIF.

3. IS ASSIGNED / IS NOT ASSIGNED

Checks if a Field Symbol is currently pointing to a memory area:

FIELD-SYMBOLS: <fs_material> TYPE mara.

IF <fs_material> IS ASSIGNED.
  WRITE: / 'Material ID:', <fs_material>-matnr.
ENDIF.

Combining Multiple Conditions (AND, OR, NOT)

The AND Operator (Short-Circuit Evaluation)

Use AND when all checked expressions must be true. SAP evaluates AND expressions from left to right using short-circuiting — if the first condition fails, SAP skips evaluating subsequent conditions:

REPORT z_and_operator_demo.

DATA: lv_age        TYPE i VALUE 25,
      lv_experience TYPE i VALUE 3.

IF lv_age >= 21 AND lv_experience >= 2.
  WRITE 'Candidate is Eligible'.
ELSE.
  WRITE 'Candidate does not meet requirements'.
ENDIF.

The OR Operator

Use OR when at least one matching expression is enough to trigger the block:

REPORT z_or_operator_demo.

DATA lv_city TYPE string VALUE 'Delhi'.

IF lv_city = 'Delhi' OR lv_city = 'Mumbai'.
  WRITE 'Metro City Operations'.
ENDIF.

Modern ABAP 7.40+ Conditional Expressions: COND & SWITCH

In Modern ABAP (7.40+), traditional IF / ELSEIF / ELSE logic can be written inline as functional expressions using COND or SWITCH.

1. Inline COND Expression

Replaces IF / ELSEIF / ELSE blocks inside assignments:

" Traditional IF ELSEIF
DATA lv_status_text TYPE string.
IF lv_status = 'A'.
  lv_status_text = 'Active'.
ELSEIF lv_status = 'I'.
  lv_status_text = 'Inactive'.
ELSE.
  lv_status_text = 'Pending'.
ENDIF.

" Modern ABAP 7.40+ COND Expression
DATA(lv_status_text) = COND string(
  WHEN lv_status = 'A' THEN 'Active'
  WHEN lv_status = 'I' THEN 'Inactive'
  ELSE 'Pending'
).

2. Inline SWITCH Expression

Evaluates exact single-variable matches efficiently:

DATA(lv_region) = SWITCH string( p_country
  WHEN 'US' THEN 'North America'
  WHEN 'DE' THEN 'Europe'
  WHEN 'IN' THEN 'Asia Pacific'
  ELSE 'Other Region'
).

Practical Business Scenario: Order Discounts

Imagine you are configuring rules for the Sales and Distribution (SD) module. The company wants to calculate discounts based on the order value:

  • Orders above 50,000 ➔ 15% Discount
  • Orders above 25,000 ➔ 10% Discount
  • Orders above 10,000 ➔ 5% Discount
  • Otherwise ➔ No Discount
REPORT z_calculate_discount.

PARAMETERS p_value TYPE p DECIMALS 2.
DATA lv_discount TYPE p DECIMALS 2.

IF p_value > 50000.
  lv_discount = p_value * '0.15'.
ELSEIF p_value > 25000.
  lv_discount = p_value * '0.10'.
ELSEIF p_value > 10000.
  lv_discount = p_value * '0.05'.
ELSE.
  lv_discount = 0.
ENDIF.

WRITE: 'Total Value:', p_value,
     / 'Discount   :', lv_discount.

Self-Assessment Checkpoints

🙋‍♂️ Checkpoint 1: What is the difference between ELSE and ELSEIF?

ELSEIF evaluates an additional explicit condition when the initial IF statement fails. ELSE acts as a default catch-all block that executes if all preceding IF and ELSEIF conditions resolve to false.

🙋‍♂️ Checkpoint 2: When should you use IS INITIAL vs IS BOUND?

Use IS INITIAL to check if standard data objects, variables, or tables are empty. Use IS BOUND to verify if an Object Reference (TYPE REF TO) points to an instantiated class object.

🙋‍♂️ Checkpoint 3: What is the difference between COND and SWITCH in modern ABAP?

COND allows complex boolean logical expressions (WHEN x > 10 AND y = 'A'), whereas SWITCH evaluates single-variable value matches (WHEN 'US' THEN ...).


Best Practices for Clean Code

  1. Use Guard Clauses: Exit routines early (RETURN or EXIT) when input validations fail instead of wrapping your entire 200-line program in a massive nested IF block.
  2. Prefer IS NOT INITIAL Over <> '': Using IS NOT INITIAL works across all data types (dates, numbers, strings, tables), whereas <> '' only applies to string characters.
  3. Keep Nesting Under 3 Levels: Deeply nested IF statements make debugging difficult. Use AND/OR operators or modularize routines into sub-methods to flatten execution logic.

Summary

Conditional logic is the foundation of clean, dynamic software in SAP ABAP. Mastering comparison operators, string patterns (CS, CP), state checks (IS INITIAL, IS BOUND), and modern 7.40+ COND/SWITCH expressions enables you to build robust, maintainable enterprise applications.

Daksh Dedha - SAP Technical Consultant

Written by Daksh Dedha

SAP Technical Consultant

Daksh is an SAP Technical Consultant specializing in ABAP programming, SAP S/4HANA migrations, Fiori development, and BTP cloud architecture. He authors free, hands-on tutorials to make enterprise SAP education accessible to all developers.

Test Your Knowledge

SAP Challenge Question 1 of 10

Loading question...

Found this tutorial useful? Share it with your SAP development team.