Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

7.11.3 CASE Examples

Basic CASE Processing

Why use the CASE command?

...

Using the CASE command with compound expressions

Basic CASE Processing

This example illustrates the most basic type of CASE command processing. 

...

Code Block
BEGIN_LOOP
REQUEST    FIELDS(#DEPTMENT (#SECTION *OUTPUT))
CASE       OF_FIELD(#DEPTMENT)
WHEN       VALUE_IS('= ADM')
CHANGE     FIELD(#SECTION) TO(A)
MESSAGE    MSGTXT('ADM was entered')
WHEN       VALUE_IS('= AUD')
CHANGE     FIELD(#SECTION) TO(B)
MESSAGE    MSGTXT('AUD was entered')
WHEN       VALUE_IS('= FLT')
CHANGE     FIELD(#SECTION) TO(C)
MESSAGE    MSGTXT('FLT was entered')
WHEN       VALUE_IS('= TRVL')
CHANGE     FIELD(#SECTION) TO(D)
MESSAGE    MSGTXT('TRVL was entered')
OTHERWISE 
CHANGE     FIELD(#SECTION) TO(E)
MESSAGE    MSGTXT('Other value was entered')
ENDCASE   
END_LOOP  

Why use the CASE command?

Imagine you have to implement this logic:

...

Which is shorter, easier to read, and easier to maintain. It also executes faster in most situations.

Using the CASE command with OR operations

Imagine that when field #TEST contains A or B or C you have to perform some operation.

...

Code Block
CASE       OF_FIELD(#TEST)

WHEN       VALUE_IS('= A' '= B' '= C')

          << Logic Block 1 >>

WHEN       VALUE_IS('= X' '= Y' '= Z')

          << Logic Block 2>>

OTHERWISE 

          << Logic Block 3>>

ENDCASE   

Using the CASE command with operations other than "=" (equal to)

Imagine that when field #COPIES needs to be validated according to these rules:

...

Code Block
DEFINE     FIELD(#COPIES) TYPE(*DEC) LENGTH(3) DECIMALS(0) EDIT_CODE(N) DEFAULT(+1)
BEGIN_LOOP
REQUEST    FIELDS(#COPIES)
CASE       OF_FIELD(#COPIES)
WHEN       VALUE_IS('= 0')
MESSAGE    MSGTXT('Value of zero is invalid')
WHEN       VALUE_IS('< 0')
MESSAGE    MSGTXT('Value is negative')
WHEN       VALUE_IS('<= 50')
MESSAGE    MSGTXT('Value is sensible')
OTHERWISE 
MESSAGE    MSGTXT('Value is probably incorrect')
ENDCASE   
END_LOOP  

Using the CASE command with compound expressions

In a situation whereby a field #DISCOUNT needs to be validated by using a mathematical calculation such as this:

...