ABAP Variables and Conditions
Declaring a variable and branching on it are the first two things you do in any language. ABAP's versions have a few sharp edges that are worth meeting deliberately rather than discovering in a debugger — chiefly that its character types are fixed length.
Declaring variables with DATA
DATA declares a variable, and TYPE gives it a type.
VALUE sets an initial value. Without VALUE,
a variable starts at its type's initial value — 0 for numbers, blank for characters —
never null. ABAP has no null for elementary types.
DATA lv_score TYPE i VALUE 85. " integer
DATA lv_grade TYPE c LENGTH 1. " one character
DATA lv_name TYPE string. " variable-length text
DATA lv_price TYPE p LENGTH 8 DECIMALS 2. " packed decimal — use for money
DATA lv_active TYPE abap_bool. " 'X' or space, not true/false
Since 7.40 you can also declare inline, with the type inferred from the right-hand side. This is preferred in modern code because the declaration sits where the value is produced:
DATA(lv_total) = 10 + 5. " typed i
DATA(lv_label) = |Total|. " typed string
The fixed-length surprise
c LENGTH n is exactly n characters — always. Assign something longer and
the rest is silently truncated; assign something shorter and it is
padded with blanks. No error, no warning.
DATA lv_short TYPE c LENGTH 3.
lv_short = 'ABAP'.
WRITE lv_short. " prints ABA — the P is gone
DATA lv_text TYPE string.
lv_text = 'ABAP'.
WRITE lv_text. " prints ABAP
This is also why DATA lv_x. with no type at all is a trap: it means
TYPE c LENGTH 1, so lv_x = 150 leaves you
with 1. See
untyped declarations.
Which to use: string for text of unknown length, fixed
c LENGTH n when the field has a defined width in the data model
(country codes, document numbers, flags) — that is what a DDIC field will expect. Both matter; picking
the wrong one is a common source of AI-generated bugs, covered in
STRING vs CHAR.
IF, ELSEIF, ELSE
Branching reads almost like prose. Note ENDIF — ABAP closes blocks
explicitly rather than by indentation or braces — and that comparison uses
=, not ==. ABAP also accepts the word
forms EQ, GT,
LE, which you will meet in older code.
IF lv_score >= 90.
lv_grade = 'A'.
ELSEIF lv_score >= 80.
lv_grade = 'B'.
ELSEIF lv_score >= 70.
lv_grade = 'C'.
ELSE.
lv_grade = 'F'.
ENDIF.
Two conditions worth knowing early, because they have no direct equivalent elsewhere:
IS INITIAL (the value equals its type's initial value) and
IS BOUND (a reference points at an object).
IF lv_name IS INITIAL.
WRITE / 'No name given'.
ENDIF.
IF lo_object IS BOUND.
WRITE / 'Reference is usable'.
ENDIF.
CASE for one value, many outcomes
CASE compares one value against several alternatives.
WHEN OTHERS catches everything else — always include it, because a
CASE that silently matches nothing is hard to spot later.
There is no fall-through, so no break is needed.
CASE lv_grade.
WHEN 'A'.
WRITE / 'Excellent!'.
WHEN 'B'.
WRITE / 'Good job!'.
WHEN 'C' OR 'D'.
WRITE / 'Passed.'.
WHEN OTHERS.
WRITE / 'Keep trying!'.
ENDCASE.
When all you want is a value rather than statements, the COND and
SWITCH expressions say it in one line — see
modern ABAP syntax.
Full example
Everything above in one runnable program: declare, branch, then dispatch on the result.
Run it, then change lv_score and run it again.
REPORT ztest_cond.
DATA lv_score TYPE i VALUE 85.
DATA lv_grade TYPE c LENGTH 1.
IF lv_score >= 90.
lv_grade = 'A'.
ELSEIF lv_score >= 80.
lv_grade = 'B'.
ELSEIF lv_score >= 70.
lv_grade = 'C'.
ELSE.
lv_grade = 'F'.
ENDIF.
WRITE: 'Score:', lv_score.
WRITE: / 'Grade:', lv_grade.
CASE lv_grade.
WHEN 'A'.
WRITE / 'Excellent!'.
WHEN 'B'.
WRITE / 'Good job!'.
WHEN OTHERS.
WRITE / 'Keep trying!'.
ENDCASE.