Operators in ABAP
Master arithmetic, comparison, logical, and string operators in ABAP - including business-focused features like the &&-concatenation, IS INITIAL, and string templates.
Operators are the building blocks of every ABAP expression - from calculating an order total in SAP S/4HANA to comparing posting dates in a financial report. Because ABAP was designed for business applications, it offers two parallel sets of comparison operators (symbolic like = and word-style like EQ), an explicit integer-division operator (DIV), and a chainable string-concatenation operator (&&) that is unusually safe with trailing blanks.
ABAP is a statically and strongly typed multi-paradigm language. Numeric operators automatically widen to the largest operand type, so mixing an integer (i) and a packed decimal (p) produces a packed result - a feature that was practically required for currency arithmetic in SAP R/3. Modern ABAP (7.40+) added string templates with embedded expressions, dramatically reducing the verbosity of formatted output.
In this tutorial you will see arithmetic, comparison, logical, string, and assignment operators - plus ABAP’s special “is” predicates such as IS INITIAL - all running on Node.js via the open-abap transpiler.
A Single Comprehensive Example
We will use one program that exercises every operator family. Because the open-abap Docker pipeline copies hello.abap into the project as the report source, we keep the same filename convention here.
Create a file named hello.abap:
| |
Notes on the Code
Arithmetic: / versus DIV
ABAP distinguishes real division from integer division:
/performs real division. When the result is assigned to an integer field, ABAP rounds (commercial rounding) - so17 / 4would store4inlv_r.DIVtruncates toward zero, giving the integer quotient directly:17 DIV 4 = 4.MODreturns the remainder:17 MOD 4 = 1.
For currency math you would normally use the packed type p DECIMALS n, which preserves cents exactly.
Symbolic vs. word-style comparisons
ABAP exposes two equivalent vocabularies, a holdover from its COBOL-influenced early years. Both compile to the same operation:
| Symbolic | Word | Meaning |
|---|---|---|
= | EQ | equal |
<> | NE | not equal |
< | LT | less than |
> | GT | greater than |
<= | LE | less or equal |
>= | GE | greater or equal |
Style guides at most SAP shops prefer the symbolic forms in modern code.
&& versus the older &
The && operator (added in ABAP 7.02) concatenates strings while ignoring trailing blanks of fixed-length character fields. Older code uses & inside a single CONCATENATE ... INTO ... statement, which behaves differently and requires RESPECTING BLANKS to keep padding.
IS INITIAL
Rather than comparing against a sentinel, ABAP exposes the predicate IS INITIAL, which is true when the variable holds the type-default value: '' for strings, 0 for numerics, 00000000 for dates, an empty internal table, and so on. The complement is IS NOT INITIAL.
Parentheses need spaces
ABAP’s parser requires whitespace around ( and ) when they group sub-expressions: ( 2 + 3 ) * 4 is valid, (2 + 3) * 4 is not. This trips up newcomers from C-family languages.
Running with Docker
| |
The pipeline:
- Mounts the current directory at
/work. - Creates a temporary npm project and installs
@abaplint/transpiler-cliplus@abaplint/runtime. - Stages
hello.abapassrc/zhello.prog.abapwith the abapGit XML metadata that the transpiler expects. - Transpiles ABAP to ES modules under
output/. - Boots the ABAP runtime in Node.js and dynamically imports the compiled program.
Expected Output
17 + 4 = 21
17 - 4 = 13
17 * 4 = 68
17 DIV 4 = 4
17 MOD 4 = 1
17 ** 2 = 289
counter after + 5: 15
a > b is true (symbolic form)
a GT b is true (word form)
a <> b is true
a EQ 17 is true
AND: both conditions hold
OR: at least one condition holds
NOT: a is not zero
Hello, ABAP!
Welcome, World!
lv_empty IS INITIAL
lv_zero IS INITIAL (value 0)
( 2 + 3 ) * 4 = 20
2 + 3 * 4 = 14
Key Concepts
- Two comparison vocabularies: symbolic (
=,<>,<) and word-form (EQ,NE,LT) are interchangeable - prefer symbolic in modern code. /rounds,DIVtruncates: real division assigned to an integer field is rounded; useDIVfor explicit integer division andMODfor the remainder.**is exponentiation: ABAP uses double-asterisk for power, not^.&&concatenates strings without surprises around trailing blanks; older code usesCONCATENATE ... INTO.- String templates
|text { expr }|(ABAP 7.40+) embed expressions inline and replace most uses ofCONCATENATEfor formatted output. IS INITIALis the idiomatic ABAP test for “default value” - cleaner than comparing against''or0.- Logical operators are word-form only:
AND,OR,NOT- there is no&&or||for booleans (those symbols are reserved for strings). - Parentheses need surrounding spaces:
( 2 + 3 ) * 4is the only valid spelling - a parser quirk inherited from ABAP’s report-language origins.
Comments
Loading comments...
Leave a Comment