SAP Tutorials Blog


 


"The following example performs a calculation (no error handling implemented)

"if a valid operator is passed, and stores the result in a data object of

"type string.

"Note: Similar to the COND operator, the THROW addition is also possible

"for the SWITCH operator.

DATA(operator) = '*'.

DATA(num1) = 5.

DATA(num2) = 7.

DATA(calc_result) = SWITCH #( operator

                              WHEN '+' THEN |{ num1 + num2 STYLE = SIMPLE }|

                              WHEN '-' THEN |{ num1 - num2 STYLE = SIMPLE }|

                              WHEN '*' THEN |{ num1 * num2 STYLE = SIMPLE }|

                              WHEN '/' THEN |{ CONV decfloat34( num1 / num2 ) STYLE = SIMPLE }|

                              ELSE `Wrong operator.` ).


"A constructor expression as above instead of, for example, a CASE statement as follows.

CASE operator.

  WHEN '+'.

   calc_result = |{ num1 + num2 STYLE = SIMPLE }|.

  WHEN '-'.

   calc_result = |{ num1 - num2 STYLE = SIMPLE }|.

  WHEN '*'.

   calc_result = |{ num1 * num2 STYLE = SIMPLE }|.

  WHEN '/'.

   calc_result = |{ CONV decfloat34( num1 / num2 ) STYLE = SIMPLE }|.

  WHEN OTHERS.

   calc_result = `Wrong operator.`.

ENDCASE.


Serkan AKKAVAK

Computer Engineer BSc

SAP Department Manager

Contact : serkurumsal@yandex.com