ABAP New Syntax Lecture 7: COND and SWITCH Expressions with Practical Examples

ABAP NEW SYNTAX COURSE
LECTURE 7
COND + SWITCH
Expression-oriented conditional logic

COND and SWITCH return values from conditional logic, so they can be used directly in assignments, constructor expressions and method parameters. They are modern expression alternatives to small, value-producing IF and CASE blocks.

Learning outcome: Choose COND for logical conditions, SWITCH for one-value case distinctions, control result types, handle default branches and keep expressions readable.

1. Replace a value-producing IF with COND

Classic
IF lv_amount >= 1000.
  lv_level = 'HIGH'.
ELSE.
  lv_level = 'NORMAL'.
ENDIF.
Modern expression
DATA(lv_level) = COND string(
  WHEN lv_amount >= 1000
  THEN 'HIGH'
  ELSE 'NORMAL' ).

COND evaluates conditions from top to bottom and returns the result of the first true WHEN branch.

2. Multiple business rules

CODE SCREEN 1 — COND
DATA(lv_discount) = COND decfloat34(
  WHEN lv_amount >= 5000 THEN CONV decfloat34( '0.15' )
  WHEN lv_amount >= 1000 THEN CONV decfloat34( '0.10' )
  ELSE CONV decfloat34( 0 ) ).

Order matters: place the most specific or highest-priority rule first. All selected results must be convertible to the expression result type.

3. Type inference with #

DATA lv_text TYPE string.

lv_text = COND #(
  WHEN lv_ready = abap_true THEN 'Ready'
  ELSE 'Pending' ).

Use # only when the operand position determines a complete type. An explicit type is often clearer in inline declarations and mixed numeric expressions.

4. SWITCH for one operand

CODE SCREEN 2 — SWITCH
DATA(lv_status_text) = SWITCH string( lv_status
  WHEN 'O' THEN 'Open'
  WHEN 'C' THEN 'Completed'
  WHEN 'X' THEN 'Cancelled'
  ELSE 'Unknown' ).

SWITCH is ideal when one source value is compared with several alternatives. COND is better when each branch needs a different logical expression.

5. Use expressions inside VALUE

DATA(ls_display) = VALUE ty_display(
  order_id = ls_order-id
  criticality = COND #( WHEN ls_order-overdue = abap_true
                         THEN 1 ELSE 3 )
  status_text = SWITCH #( ls_order-status
                          WHEN 'O' THEN 'Open'
                          WHEN 'C' THEN 'Completed'
                          ELSE 'Unknown' ) ).

6. Raise an exception from a branch

DATA(lv_currency) = COND waers(
  WHEN ls_order-currency IS NOT INITIAL
  THEN ls_order-currency
  ELSE THROW cx_sy_conversion_no_number( ) ).

A THROW expression can make an invalid state explicit. Use the exception class that correctly represents your application error and is permitted in the current context.

7. Defaults and readability

  • If ELSE is omitted and no WHEN matches, the expression returns the initial value of its result type.
  • Prefer an explicit ELSE when an initial value could hide a defect.
  • Do not compress complex business workflows into nested COND and SWITCH expressions.
  • Extract repeated or domain-heavy decisions into well-named methods.
  • Keep branch results short; the expression should read like a decision table.
Common mistake: Using SWITCH when each WHEN needs unrelated conditions. SWITCH performs a case distinction on one operand; COND accepts general logical expressions.

8. Practical exercises

  1. Return Gold, Silver or Standard from a sales amount using COND.
  2. Map single-character delivery statuses to text using SWITCH.
  3. Embed both expressions in a VALUE structure.
  4. Add an explicit exception branch for an unsupported status and test it.

9. Interview quick checks

What is the core difference between COND and SWITCH?

COND evaluates independent logical expressions. SWITCH compares one operand against multiple alternatives.

Which branch wins in COND?

The result following the first true WHEN branch is selected.

What happens without ELSE?

If no condition or case matches, the initial value of the result type is produced.

Can these expressions be passed directly to a method?

Yes, when their result type is compatible with the formal parameter.

Official SAP references

Educational examples. Validate syntax, exception classes and type inference on your target ABAP release.

Post a Comment

0 Comments