IF and CASE — the statements¶
Level: 101 · newcomer
Status: stub — the shape is here; the worked program is not yet.
One line: IF/ELSEIF/ELSE tests conditions and CASE/WHEN compares one operand against values, exactly as elsewhere — the ABAP-specific parts are the word operators (IS INITIAL, IS BOUND, IS ASSIGNED, BETWEEN, CO, CS) and the fact that CASE cannot express a range.
What it does¶
IF lv_amount > 0 AND lo_handler IS BOUND.
ELSEIF lv_amount IS INITIAL.
ELSE.
ENDIF.
CASE lv_status.
WHEN 'A' OR 'B'.
WHEN OTHERS.
ENDCASE.
CASE … WHEN OTHERS is not required, and leaving it out is how an unexpected status does nothing at all rather than being reported. WHEN TYPE lcl_thing (7.02+) branches on an object's class, which is occasionally the right tool and usually a sign that the class hierarchy is missing a method.
The operators worth knowing¶
IS INITIAL (the value is its type's initial value — not null, ABAP has no null), IS BOUND (a reference points at something), IS ASSIGNED (a field symbol points at something), IS SUPPLIED (an optional parameter was actually passed), BETWEEN a AND b, and the character-comparison family CO, CN, CA, NA, CS, NS, CP, NP — of which CS (contains string) and CP (covers pattern, with * and +) are the two that earn their keep.
What this page still needs¶
- [ ] a
snippets/program exercisingIS INITIALagainstIS BOUNDagainstIS ASSIGNED - [ ] the
CO/CA/CSfamily with a worked example each, andsy-fdpos - [ ] when
CASE TYPE OFis right and when it is a missing method
See also¶
CONDandSWITCH— the same two choices as expressionsCHECK,CONTINUE,EXIT,RETURN— leaving early instead of nestingFIELD-SYMBOLSandASSIGN— whyIS ASSIGNEDis a different question fromIS INITIALCASE TYPE OF— branching on an object's class, and when not to- Booleans —
xsdbool( ),boolc( ), and why there is no boolean type