Conditions in ABAP #
IF … ENDIF #
Objectives #
- Understand how the
IFstatement works in ABAP - Know how to test one or more conditions with
IF,ELSEIFandELSE - Learn to combine conditions with
ANDandOR - Prevent errors and unexpected behaviors (e.g., division by zero)
- Know how to properly organize conditions to respect
AND/ORpriority
Definition #
The
IFstatement allows testing a condition on a variable and executing processing only if this condition is true. It can be completed withELSEIFto test other conditions andELSEto handle all remaining cases.
Like a pedestrian crossing: if the light is green, you cross; if the light is orange, you slow down; otherwise you wait.
IFis the simplest control structure to handle conditional logic in ABAP. Proper use ofANDandORallows efficiently combining multiple logical conditions.
Syntax #
IF log_exp1.
[statement_block1]
[ELSEIF log_exp2.
[statement_block2]]
...
[ELSE.
[statement_blockn]]
ENDIF.
log_exp: logical expression to testELSEIF: allows testing another condition if the first is falseELSE: executes processing if all previous conditions are false
IF/ELSEIF/ELSEblocks must be properly indented for better code readability.
Examples #
Simple Example #
WRITE:/ ' - SIMPLE IF...'.
DATA: lv_a TYPE I,
lv_b TYPE I,
lv_c TYPE I.
lv_b = 3.
lv_c = 2.
lv_a = lv_b + lv_c.
IF lv_a = 5.
WRITE:/ 'Condition confirmed, lv_a = 5'.
ENDIF.
Check if an object has the expected value before proceeding to the next action.
With ELSEIF and ELSE #
WRITE:/ ' - IF ELSEIF...'.
DATA lv_month TYPE I.
lv_month = SY-DATUM+4(2).
IF lv_month = 1.
WRITE:/'It''s January'.
ELSEIF lv_month = 2.
WRITE:/'It''s February'.
ELSEIF lv_month = 3.
WRITE:/'It''s March'.
ELSEIF lv_month = 4.
WRITE:/'It''s April'.
ELSEIF lv_month = 5.
WRITE:/'It''s May'.
ELSE.
WRITE:/'Another month of the year'.
ENDIF.
ELSEIFavoids stacking multiple nested IFs and makes code more readable.
Using AND / OR #
WRITE:/ ' - IF ELSEIF AND/OR...'.
DATA: lv_month TYPE i,
lv_day TYPE i.
lv_month = SY-DATUM+4(2).
lv_day = SY-DATUM+6(2).
IF lv_month = 12 OR lv_month = 1 OR lv_month = 2.
WRITE:/'It''s winter'.
ELSEIF lv_month = 3 OR lv_month = 4 OR lv_month = 5.
WRITE:/'It''s spring'.
ELSEIF lv_month = 6 OR lv_month = 7 OR lv_month = 8.
WRITE:/'It''s summer'.
ELSEIF lv_month = 9 OR lv_month = 10 OR lv_month = 11.
WRITE:/'It''s autumn'.
ENDIF.
IF lv_month = 1 AND lv_day = 1.
WRITE:/'Happy New Year'.
ELSEIF lv_month = 12 AND lv_day = 25.
WRITE:/'Merry Christmas'.
ELSEIF lv_month = 5 AND lv_day = 1.
WRITE:/'Happy Labor Day'.
ELSE.
WRITE:/'Have a good day'.
ENDIF.
The
ANDoperator checks that all conditions are true, whileORchecks that at least one condition is true.
AND / OR Priority #
The AND operator always has priority over OR.
To avoid any ambiguity, use parentheses.
WRITE:/ ' - IF PRIORITY...'.
IF ( lv_month = 1 OR lv_month = 2 ) AND lv_day = 1.
WRITE:/ 'New Year or other event in January/February'.
ENDIF.
Always clarify logic with parentheses to avoid unexpected results.
Avoiding a Dump #
WRITE:/ ' - PREVENTIVE ANTI-DUMP IF...'.
DATA: lv_a TYPE I,
lv_b TYPE I,
lv_result TYPE I.
lv_a = 5.
lv_b = 0.
IF lv_b > 0.
lv_result = lv_a / lv_b.
WRITE lv_result.
ELSE.
WRITE 'The value of lv_b is equal to 0, operation impossible'.
ENDIF.
Check conditions to avoid runtime errors, like division by zero.
Summary #
IFtests a condition and executes the block if it’s true.ELSEIFallows testing alternative conditions.ELSEcaptures all other uncovered cases.AND/ORallow combining multiple conditions.- Always use control to avoid errors like division by zero.
[!TIP] Like traffic lights and signs: depending on the situation, the action changes.
Comparison Operators #
Objectives #
- Understand the role of comparison operators in ABAP
- Know how to apply operators to test equality, difference and order relations
- Know how to use keywords (
EQ,GT,LE, etc.) in ABAP statements
Definition #
Comparison operators allow testing conditions between two values. They can be used for all data types: numeric, alphanumeric or text.
These operators are mainly used in
IF,WHILEstatements or any logical condition. They return true or false based on the comparison result.
Operator Table #
| Operation | Sign | Keyword | Meaning |
|---|---|---|---|
| Equal | = |
EQ |
EQual |
| Strictly greater than | > |
GT |
Greater Than |
| Greater than or equal to | >= |
GE |
Greater or Equal |
| Strictly less than | < |
LT |
Lower Than |
| Less than or equal to | <= |
LE |
Lower or Equal |
| Different | <> |
NE |
Not Equal |
=orEQchecks strict equality>orGTand<orLTcompare numeric or alphabetic values>=orGEand<=orLEinclude equality in comparison<>orNEchecks that two values are different
Like measuring and comparing two objects to know which is larger, smaller, identical or different.
Prefer keywords (
EQ,NE,GT, etc.) in ABAP statements for better readability and code clarity, especially when read by other developers.
CASE … ENDCASE #
Objectives #
- Understand how the
CASEstatement works in ABAP - Know how to test a variable and execute processing based on its value
- Learn to use
WHEN,ORandWHEN OTHERSto handle all possible cases - Avoid unexpected behaviors by providing default processing
Definition #
The CASE statement allows checking the value of a (single) variable and executing different processing based on this value.
Like a traffic light: depending on the color (value), you perform a different action (stop, go, slow down).
CASEis often more readable than a series ofIF ... ELSEIF ... ELSEwhen you have multiple values to test for the same variable.
Syntax #
CASE operand.
[WHEN operand1 [OR operand2 [OR operand3 [...]]].
[statement_block1]]
...
[WHEN OTHERS.
[statement_blockn]]
ENDCASE.
operand: variable to testWHEN: defines one or more values and associated processingOR: allows associating multiple values for the same processingWHEN OTHERS: optional, captures all unforeseen cases
Always provide a
WHEN OTHERSto avoid unexpected behaviors if the variable value is not in the defined cases.
Prefer
CASEwhen multiple values of the same variable must trigger distinct processing, for better code readability and maintenance.
Example #
WRITE:/ ' - CASE ENDCASE...'.
DATA: lv_month TYPE i.
lv_month = SY-DATUM+4(2).
CASE lv_month.
WHEN 12 OR 1 OR 2.
WRITE:/'It''s winter'.
WHEN 3 OR 4 OR 5.
WRITE:/'It''s spring'.
WHEN 6 OR 7 OR 8.
WRITE:/'It''s summer'.
WHEN 9 OR 10 OR 11.
WRITE:/'It''s autumn'.
WHEN OTHERS.
WRITE:/'No more seasons!'.
ENDCASE.
The program reads the
lv_monthvariable as if reading a menu choice: depending on the value, a specific action is performed.
Summary #
CASEtests a variable to execute different processing based on its value.WHENdefines possible values and associated processing.ORallows grouping multiple values under the same processing.WHEN OTHERScaptures all unforeseen values to avoid errors.[!TIP] Like a menu with multiple options: depending on the user’s choice, a different action will be executed.
IS INITIAL #
Objectives #
- Understand using
IS INITIALto test if a variable is empty or contains its default value - Know how to use
IS NOT INITIALto check that a variable contains a value - Apply
IS INITIALwithIFfor simple conditions - Apply
IS INITIALwithCASEto handle multiple cases - Simplify tests for uninitialized or empty variables
Definition #
The IS INITIAL statement allows testing whether a variable is initialized or not.
IS INITIALreturns true if the variable is empty or contains the default value for its type.IS NOT INITIALreturns true if the variable contains a value.
Like checking if a box is empty or contains something
Very practical to avoid errors when working on variables that may not yet be filled
Syntax #
With IF #
IF operand IS [NOT] INITIAL.
[statement_block]
ENDIF.
With CASE #
CASE operand.
[WHEN IS [NOT] INITIAL].
[statement_block1]
...
[WHEN OTHERS].
[statement_blockn]
ENDCASE.
IS INITIALworks for all variable types: strings, numbers, dates, times, internal tables.
IF IS INITIAL #
WRITE:/ ' - IS INITIAL + IF...'.
DATA: lv_text TYPE string,
lv_num TYPE i.
lv_text = ''.
lv_num = 0.
IF lv_text IS INITIAL.
WRITE:/ 'The lv_text variable is empty'.
ENDIF.
IF lv_num IS NOT INITIAL.
WRITE:/ 'The lv_num variable contains a value'.
ENDIF.
Check a box before using it: if it’s empty, you can fill it, otherwise you read its content
Summary #
IS INITIALtests if a variable is empty or contains its default value.IS NOT INITIALtests if the variable contains a value.- Works for all variable types: strings, numbers, dates, internal tables.
[!TIP] Like checking if a box is empty or filled before acting on its content
BETWEEN #
Objectives #
- Understand using
BETWEENto test if a variable is in a range - Know how to apply
BETWEEN low AND highwithIF - Know how to apply
BETWEEN low AND highwithCASE - Simplify numeric comparisons using
BETWEEN - Combine
BETWEENwith other tests likeIS INITIALfor robust conditions
Definition #
The BETWEEN statement allows testing if a variable is within a value range.
BETWEEN low AND highreturns true if the variable is betweenlowandhighinclusively.- Can be used with
IForCASE.
Like checking if a value is between two boundaries on a graduated ruler
Ideal for testing score intervals, ages, or any numeric variable that must respect a specific range
Syntax #
With IF #
WRITE:/ ' - BETWEEN + IF...'.
DATA: lv_variable TYPE i.
IF lv_variable BETWEEN 1 AND 100.
WRITE: 'The lv_variable is between 1 and 100.'.
ELSE.
WRITE: 'The lv_variable is not between 1 and 100.'.
ENDIF.
Rather than writing
IF lv_variable >= 1 AND lv_variable <= 100,BETWEENsimplifies reading and reduces error risk.
With CASE #
WRITE:/ ' - BETWEEN + CASE...'.
DATA: lv_variable TYPE i.
CASE lv_variable.
WHEN BETWEEN 1 AND 100.
WRITE: 'The lv_variable is between 1 and 100.'.
WHEN OTHERS.
WRITE: 'The lv_variable is not between 1 and 100.'.
ENDCASE.
BETWEENis compatible withWHENin aCASE, which makes blocks more readable for numeric ranges.
Summary #
BETWEEN low AND hightests if a variable is between two inclusive values.- Works with
IFandCASE.- Simplifies comparative conditions on numeric ranges.
[!TIP] Like checking if a value is between two boundaries on a graduated ruler, more readable and less error-prone than multiple separate comparisons
CONTAINS ONLY (CO) #
Objectives #
- Understand the
CO(Contains Only) operator and its utility - Check that a string contains only authorized characters
- Know how to use
IF ... CO ... ENDIFfor simple controls - Know the case sensitivity of
CO - Use
SY-FDPOSto know the position of the last checked character - Apply
COto validate or filter user inputs
Definition #
The CO (Contains Only) operator allows checking that a character string contains only certain characters defined in another string.
It’s like checking that the letters of a word all belong to a given alphabet.
“HELLO” contains only letters from A to Z → true. “HELLO1” contains a digit → false.
Ideal for controlling user inputs or validating that codes contain only authorized characters
Syntax #
IF oper1 CO oper2.
[statement_block]
ENDIF.
oper1→ the string to analyze.oper2→ authorized characters inoper1.- If
oper1contains only characters present inoper2, the condition is true.
The
COoperator checks each character ofoper1and returns TRUE only if all are present inoper2. The position of the last checked character is stored inSY-FDPOS.
Basic Example #
WRITE:/ ' - IF CONTAINS ONLY...'.
CONSTANTS: lc_oper1 TYPE CHAR5 VALUE 'Hello',
lc_oper2 TYPE CHAR5 VALUE 'Hello'.
IF lc_oper1 CO lc_oper2.
WRITE:/ 'lc_oper1 contains only characters from ', lc_oper2,
' at position ', SY-FDPOS.
ELSE.
WRITE:/ 'lc_oper1 does not contain only characters from ', lc_oper2.
ENDIF.
Explanation #
lc_oper1andlc_oper2contain'Hello'.IF lc_oper1 CO lc_oper2.checks that all characters oflc_oper1are inlc_oper2.- Here the condition is true, and
SY-FDPOS = 5indicates the last character position. - If
lc_oper2was'Hell', the condition would be false.
COis case-sensitive:'HELLO'≠'Hello'.- Very useful for validating that entered codes or strings respect an alphabet or a set of authorized characters.
Summary #
CO= Contains Only → tests if all characters of a string belong to an authorized set.- Returns TRUE if the string respects the set, FALSE otherwise.
- The position of the last checked character is available in
SY-FDPOS.- Ideal for validating or filtering user inputs.
[!TIP] Check that all ingredients of a dish come from a single authorized list, without intruders.
CONTAINS ANY (CA) #
Objectives #
- Understand the
CA(Contains Any) operator and its functioning - Check if a string contains at least one character from another string
- Know how to use
IF ... CA ... ENDIFfor simple controls - Use
SY-FDPOSto get the position of the first character found - Apply
CAto detect the presence of specific characters in user input
Definition #
The CA (Contains Any) operator allows checking if a character string (oper1) contains at least one character present in another string (oper2).
The condition is true as soon as a single character from oper2 is found in oper1.
Imagine
oper2as a “box of letters to search for”. If you openoper1and find at least one of these letters, theCAcondition is validated.
- “HELLO” CA “XYZ” → false (no common letters)
- “HELLO” CA “AEIOU” → true (there’s “E” and “O”)
- “HELLO” CA “LLO” → true (the “L” appears very quickly)
Practical for detecting the presence of specific characters in user inputs (letters, digits, symbols)
Syntax #
IF oper1 CA oper2.
[statement_block]
ENDIF.
oper1→ the tested stringoper2→ the string containing searched characters- Condition true if at least one character from
oper2is present inoper1
The
CAoperator traversesoper1and returns TRUE as soon as it finds a character present inoper2. The system variableSY-FDPOSindicates the position of the first character found.
Example #
WRITE:/ ' - IF CONTAINS ANY...'.
CONSTANTS: lc_oper1 TYPE CHAR5 VALUE 'Hello',
lc_oper2 TYPE CHAR3 VALUE 'llo'.
IF lc_oper1 CA lc_oper2.
WRITE:/ 'lc_oper1 contains at least one character from ', lc_oper2,
' at position ', SY-FDPOS.
ELSE.
WRITE:/ 'lc_oper1 contains no character from ', lc_oper2.
ENDIF.
lc_oper1 = 'Hello',lc_oper2 = 'llo'- The condition is true because
landoare present inlc_oper1 SY-FDPOS = 2→ position of first character found
CAis case-sensitive:'HELLO'≠'Hello'- Very useful for checking the presence of digits, letters or symbols in a text field
Summary #
CA= Contains Any → checks ifoper1contains at least one character fromoper2.- Condition TRUE as soon as a single character matches
SY-FDPOSreturns the position of the first character found- Ideal for checking the presence of specific characters in a string
[!TIP] It’s like searching for a specific letter in a box to know if it’s there
CONTAINS STRING (CS) #
Objectives #
- Understand the
CS(Contains String) operator and its functioning - Check if a string contains a complete substring
- Differentiate
CS(substring) fromCA(individual character) - Learn to use
IF ... CS ... ENDIFfor simple controls - Identify when to use
FINDto get the exact position or make advanced searches
Definition #
The CS (Contains String) operator allows checking if a character string (oper1) contains a complete substring (oper2).
Unlike CA (Contains Any), which only searches for one character, CS searches for an entire sequence of characters in another string.
Imagine
oper1as a book andoper2as a specific sentence you want to find. If the exact sentence is present, theCScondition is true.
- “HELLO WORLD” CS “WORLD” → true
- “HELLO WORLD” CS “WOR” → true
- “HELLO WORLD” CS “WORD” → false (letters out of sequence)
- “HELLO WORLD” CS “hello” → false (case-sensitive)
CSis useful for checking if a text contains a specific sequence without traversing each character individually.
Syntax #
IF oper1 CS oper2.
[statement_block]
ENDIF.
oper1→ main stringoper2→ searched substring- Condition true if
oper2is present inoper1
Example #
WRITE:/ ' - IF CONTAINS STRING...'.
CONSTANTS: lc_oper1 TYPE CHAR11 VALUE 'Hello World',
lc_oper2 TYPE CHAR5 VALUE 'World'.
IF lc_oper1 CS lc_oper2.
WRITE:/ 'lc_oper1 contains the character string ', lc_oper2.
ELSE.
WRITE:/ 'lc_oper1 does not contain the character string ', lc_oper2.
ENDIF.
lc_oper1= “Hello World”lc_oper2= “World”- Condition true because “World” is a substring of “Hello World”
- Displayed result:
lc_oper1 contains the character string World
To get the exact position of the substring or perform more complex searches (case-insensitive, complete word…), use
FINDinstead.
Summary #
CS= Contains String → checks ifoper1contains the exact substringoper2.- Case-sensitive.
- Condition true if the complete sequence is present.
- For more detailed searches, use
FIND.[!TIP] It’s like searching for an exact phrase in a book rather than checking letter by letter
NO STRING (NS) #
Objectives #
- Understand the
NS(No String) operator and its functioning - Check if a string does not contain a specific substring
- Differentiate
NS(absence of substring) fromCS(presence of substring) - Learn to use
IF ... NS ... ENDIFto control absence of a sequence - Identify practical cases where checking absence is necessary
Definition #
The NS (No String) operator is the inverse of CS (Contains String).
It allows checking that a character string (oper1) does not contain another substring (oper2).
Imagine
oper1as a book andoper2as a specific sentence you’re looking for. If this sentence doesn’t exist in the text, theNScondition is true.
- “HELLO WORLD” NS “WORLD” → false
- “HELLO WORLD” NS “SAP” → true
- “HELLO WORLD” NS “hello” → true (case-sensitive)
Syntax #
IF oper1 NS oper2.
[statement_block]
ENDIF.
oper1→ main stringoper2→ searched substring- Condition true if
oper2is not present inoper1
Example #
WRITE:/ ' - IF NO STRING...'.
CONSTANTS: lc_oper1 TYPE CHAR11 VALUE 'Hello World',
lc_oper2 TYPE CHAR3 VALUE 'SAP'.
IF lc_oper1 NS lc_oper2.
WRITE:/ 'lc_oper1 does not contain the character string ', lc_oper2.
ELSE.
WRITE:/ 'lc_oper1 contains the character string ', lc_oper2.
ENDIF.
lc_oper1= “Hello World”lc_oper2= “SAP”- Condition true because “SAP” doesn’t exist in “Hello World”
- Displayed result:
lc_oper1 does not contain the character string SAP
Difference with CS #
CS(Contains String) → true if substring is foundNS(No String) → true if substring is not found
Summary #
NS= No String → checks thatoper1does not containoper2- Case-sensitive
- Logical inverse of
CS- Very useful for detecting the absence of a word or specific sequence in a string
[!TIP] It’s like checking that a specific phrase doesn’t appear in a book
COVERS PATTERN (CP) #
Objectives #
- Understand the
CP(Covers Pattern) operator and its usage - Check if a string matches a PATTERN with wildcards (* and +)
- Use
IF…CP…ENDIFfor flexible comparisons - Identify practical situations: filenames, extensions, text formats
- Recognize case sensitivity and compatible VARIABLE types (C or STRING)
Definition #
The CP (Covers Pattern) operator allows testing if a string (oper1) matches a PATTERN (oper2) that can contain wildcards (* and +).
Like a search filter in a file explorer: “*.png” returns all files ending with “.png”.
CPis case-sensitive. “Hello” doesn’t match “*hello”.
Syntax #
IF oper1 CP oper2.
[statement_block]
ENDIF.
- oper1 → STRING to test
- oper2 → PATTERN possibly containing:
-
- → replaces any sequence of characters (even empty)
-
- → replaces exactly one character
-
Use
CPto validate file formats, product codes or specific text patterns.
Examples #
WRITE:/ ' - IF COVERS PATTERN...'.
CONSTANTS: lc_oper1 TYPE CHAR9 VALUE 'image.png',
lc_oper2 TYPE CHAR5 VALUE '*.png'.
IF lc_oper1 CP lc_oper2.
WRITE:/ 'The file read is in PNG format'.
ELSE.
WRITE:/ 'The file read is not in PNG format'.
ENDIF.
Explanation:
- lc_oper1 = “image.png”
- lc_oper2 = “*.png”
- The “*” means “doesn’t matter what’s before .png”.
- Result: “The file read is in PNG format”
Other Examples #
- ‘abc123’ CP ‘abc*’ → TRUE (starts with “abc”)
- ‘abc123’ CP ‘*123’ → TRUE (ends with “123”)
- ‘abc123’ CP ‘a+*’ → TRUE (first character “a”, followed by at least one other)
- ‘hello’ CP ‘*o’ → TRUE (ends with “o”)
- ‘hello’ CP ‘*x’ → FALSE (doesn’t end with “x”)
CPonly works with VARIABLE typesCorSTRING.
Summary #
CP= COVERS PATTERN → tests matching of a STRING with a PATTERN.- Wildcards * → multiple characters, + → one character
- Case-sensitive
- Very useful for filtering files, extensions or text formats
[!TIP] Like an advanced search filter: you can say “show me all files starting with ‘abc’ and ending with ‘.txt’”.
NO PATTERN (NP) #
Objectives #
- Understand the
NP(No Pattern) operator and its role - Check if a STRING doesn’t match a PATTERN with wildcards (* and +)
- Use
IF…NP…ENDIFfor inverse comparisons - Identify practical applications: exclude files, formats or text patterns
- Recognize case sensitivity and compatible VARIABLE types (C or STRING)
Definition #
The NP (No Pattern) operator is the inverse of CP (Covers Pattern).
It allows checking if a STRING (oper1) doesn’t match the PATTERN (oper2) containing wildcards (* and +).
If
CPis like a filter that selects matching files (e.g., “*.png” to keep only images), thenNPis its opposite — it excludes everything that matches this model. It’s like saying: “I want everything except .png files”.
NPis case-sensitive. “Hello” and “hello” will be considered different.
Syntax #
IF oper1 NP oper2.
[statement_block]
ENDIF.
- oper1 → STRING to test
- oper2 → PATTERN to NOT respect (with * and + as wildcards)
Exclude certain unwanted files or formats, validate that entries don’t match a forbidden pattern.
Examples #
WRITE:/ ' - IF NO PATTERN...'.
CONSTANTS: lc_oper1 TYPE CHAR9 VALUE 'texte.txt',
lc_oper2 TYPE CHAR5 VALUE '*.png'.
IF lc_oper1 NP lc_oper2.
WRITE:/ 'The file read is not in PNG format'.
ELSE.
WRITE:/ 'The file read is in PNG format'.
ENDIF.
Explanation:
- lc_oper1 = “texte.txt”
- lc_oper2 = “*.png”
- The program tests if “texte.txt” does NOT match the pattern “*.png”.
- Result: “The file read is not in PNG format”
Other Examples #
- ‘photo.jpg’ NP ‘*.png’ → TRUE (doesn’t match pattern)
- ‘photo.png’ NP ‘*.png’ → FALSE (matches pattern)
- ‘hello’ NP ‘h+*’ → FALSE (does start with “h”)
- ‘data123’ NP ‘abc’ → TRUE (doesn’t contain “abc”)
The pattern (oper2) must be a string of type
CorSTRING.
Summary #
NP= NO PATTERN → tests that a string doesn’t match a given PATTERN.- Opposite of
CP(Covers Pattern).- Wildcards: * for multiple characters, + for one.
- Case-sensitive.
- Very useful for excluding unwanted files, formats or text patterns.
[!TIP] Like saying “I want everything except this file type or this specific pattern”.