英文原文:SAP-samples · 上游版本 d43eb13c4c7c · 原始 Markdown
Logical Expressions and Functions
- Logical Expressions and Functions
This ABAP cheat sheet covers logical expressions and functions that are used to process data based on specific conditions and to control program flow.
[!NOTE] Many topics covered here are also included in other ABAP cheat sheets. This ABAP cheat sheet is to summarize.
Introduction
Logical expressions:
- Formulate conditions for operands in ABAP statements, as well as in ABAP CDS and SQL (such as
WHEREandONconditions, which are not covered here). - Represent relational expressions, that is, expressions which result in a truth value (true or false). These include comparison expressions and predicates.
- Consist of a single relational expression or multiple relational expressions combined with Boolean operators (such as
ANDorOR). - In ABAP, they are typically used in control statements that define a control structure, such as
IF, executed based on conditions. They can also be used in other contexts, like conditional expressions withCONDandSWITCH, to defineWHEREconditions, or as arguments in Boolean functions likexsdbool. - Can involve various operands, including data objects, built-in functions, functional methods (methods with a returning parameter), and calculation, constructor, or table expressions.
Logical functions:
- Are built-in functions.
- Include Boolean and predicate functions that return a truth value.
Comparison Expressions with Comparison Operators
- Comparison expressions are relational expressions whose result is a truth value.
- They can combine two or more operands using comparison operators like
=. - The comparison follows specific rules.
[!TIP] Find more details on the comparison rules here.
[!NOTE] ABAP includes special comparison operators for byte-like data types and bit patterns, which are not covered here. For more information, refer to the ABAP Keyword Documentation.
General Comparison Operators
The following comparison operators can be used for all data types:
| Comparison Operator | Details | Example |
|---|---|---|
|
|
The content of two operands is equal. |
|
|
|
The content of two operands is not equal. |
|
|
|
The content of the left operand is less than the content of the right operand. |
|
|
|
The content of the left operand is greater than the content of the right operand. |
|
|
|
The content of the left operand is less than or equal to the content of the right operand. |
|
|
|
The content of the left operand is greater than or equal to the content of the right operand. |
|
|
|
Ternary comparison operator |
|
|
|
Tabular comparison operator |
|
Comparison Operators for Character-Like Data Types
| Comparison Operator | Details | Example |
|---|---|---|
CA |
Contains any To determine whether any character of a given character set is contained in a string. Note: The search is case-sensitive. sy-fdpos contains the offset of the first character found, while 0 stands for the very first position. If nothing is found, sy-fdpos contains the length of the string. |
|
NA |
Contains not any To determine whether any character of a given character set is not contained in a string. See the note above. |
|
CO |
Contains only To determine whether a string contains only a certain set of characters. See the note above. |
|
CN |
Contains not only To determine whether a string does not only contain a certain set of characters, i.e. whether a string contains characters other than those in the character set. See the note above. |
|
CS |
Contains string For simple substring searches and determining whether a string contains a substring. Note: The search is not case-sensitive. sy-fdpos contains the offset of the first substring found. If it is not found, sy-fdpos contains the length of the string searched. |
|
NS |
Contains no string To determine whether a substring is not contained in a string. See the note for CS. |
|
CP |
Conforms to pattern For simple pattern searches and determining whether a set of characters is contained in a string that matches a particular pattern. You can use the following special characters as patterns:
#. If a pattern is found, sy-fdpos returns the offset of the first occurrence. Otherwise, it contains the length of the string searched.
|
|
NP |
Does not conform to pattern Negation of CP. See the previous notes. |
|
Predicates
Predicates are relational expressions that determine a truth value based on an operand. In ABAP, you can find these predicates in predicate expressions, predicate functions, and predicative method calls.
Predicate Expressions
- Predicate expressions are relational expressions with the predicate operator
IS. - The addition
NOT(IS NOT) negates.
| Predicate Expression | Details | Example |
|---|---|---|
|
|
Note that you can also specify an individual data object that represents a relational expression, returning a truth value. The expression evaluates to true if the operand's content is not initial. |
|
|
|
|
|
|
|
|
The following example uses local classes and interfaces to demonstrate predicate expressions. Global class
CCIMP include (Local types tab)
|
|
|
Checks whether a memory area is assigned to a field symbol or not. |
|
|
|
|
|
Predicate Functions
- Predicate functions are built-in logical functions that return a truth value.
- ABAP supports predicate functions for character-like and table-like arguments.
| Predicate Functions | Details | Example |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Predicative Method Calls, Data Objects as Relational Expressions
| Context | Details | Example |
|---|---|---|
|
Predicative method call |
|
|
|
Data object as relational expression |
|
|
Boolean Operators and Parentheses
- Boolean operators allow to combine (
AND,OR,EQUIV) and negate (NOT) logical expressions. - You can use parentheses to refine conditions further.
| Operator | Details | Example |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Boolean Functions
Like predicate functions, Boolean functions are built-in logical functions that evaluate a logical expression and return a truth value.
[!NOTE]
The boolean functionboolxis available for byte-like types. Find more information here.
| Function | Details | Example |
|---|---|---|
boolc |
Boolean function that returns a truth value. In this case, it is a single-character value of type string. When true, it returns the string X. When false, it returns a blank. The result is not to be compared with abap_true and abap_false (because of c to string conversion). To get the technical type c (with the values 'X' and ''), you can use the xsdbool function.
|
|
xsdbool |
Boolean function that returns a truth value. Similar to boolc, it returns the value X for true, and a blank for false. Unlike boolc, the return value is of type c of length 1, and can be compared with abap_true and abap_false.
|
|
Excursion: Operands of Logical Expressions
In most cases, operands of logical expressions can include:
- Data objects
- Built-in functions
- Functional methods
- Calculation expressions
- Constructor expressions
- Table expressions
DATA(num) = 1.
DATA(str_table) = VALUE string_table( ( `a` ) ( `b` ) ( `c` ) ).
DATA result TYPE abap_boolean.
IF
"---- Data object/literals ----
num = 1
AND
'ABAP' IS NOT INITIAL
AND
"---- Built-in functions ----
reverse( `PABA` ) = `ABAP`
AND
condense( val = ` A B A P ` to = `` ) = `ABAP`
AND
repeat( val = `*` occ = 5 ) = `*****`
AND
count( val = `hello` sub = `l` ) = 2
AND
strlen( `ABAP` ) = 4
AND
ipow( base = 2 exp = 3 ) = 8
AND
xsdbool( 3 > 1 ) = abap_true
AND
"---- Functional methods ----
cl_abap_random_int=>create( seed = cl_abap_random=>seed( ) min = 1 max = 10 )->get_next( ) BETWEEN 1 AND 10
AND
cl_abap_random_int=>create( seed = cl_abap_random=>seed( ) min = 1 max = 10 )->get_next( )
AND
"---- Calculation expressions ----
1 + 2 = 3
AND
5 = ( 11 - 5 ) - 1
AND
"---- Constructor expressions ----
VALUE i( ) = 0
AND
CONV string( 'abc ' ) = `abc`
AND
COND #( WHEN num = 1 THEN `A` ELSE `B` ) = `A`
AND
"---- Table expressions ----
str_table[ 2 ] = `b`
.
result = abap_true.
ELSE.
result = abap_false.
ENDIF.
ASSERT result = abap_true.