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 OR operations

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

Using the CASE command with compound expressions

Anchor
Basic CASE Processing
Basic CASE Processing
Basic CASE Processing

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

...

Structurally, a CASE command to implement this logic would look like this:

...

   CASE       OF_FIELD(#DEPTMENT)

...


WHEN       VALUE_IS('= ADM')

...


           << Logic >>

...


WHEN       VALUE_IS('= AUD')

...


           << Logic >>

...


WHEN       VALUE_IS('= FLT')

...


           << Logic >>

...


WHEN       VALUE_IS('= TRVL')

...


          << Logic >>

...


OTHERWISE
           << Logic >>

...


ENDCASE

A sample program to fully implement this logic might be coded like this: 

...

   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 

Anchor
Why use the CASE command?
Why use the CASE command?
Why use the CASE command?

Imagine you have to implement this logic:

...

If you code nested IF-ELSE-END blocks you will end up with logic like this: 

...

   IF         COND('(#DEPTMENT = ADM) or (#DEPTMENT = FLT) or (#DEPTMENT = TRVL)')

...


CHANGE     FIELD(#SECTION) TO(A)

...


MESSAGE    MSGTXT('Either ADM , FLT or TRVL was entered')

...


ELSE     
IF         COND('(#DEPTMENT = AUD) or (#DEPTMENT = INF) or (#DEPTMENT = MKT)')

...


CHANGE     FIELD(#SECTION) TO(B)

...


MESSAGE    MSGTXT('Either AUD, INF or MKT was entered')

...


ELSE     
CHANGE     FIELD(#SECTION) TO(C)

...


MESSAGE    MSGTXT('Other value was entered')

...


ENDIF    
ENDIF

However, by using a CASE command you can code this instead:

...

   CASE       OF_FIELD(#DEPTMENT)

...


WHEN       VALUE_IS('= ADM' '= FLT' '= TRVL')

...


CHANGE     FIELD(#SECTION) TO(A)

...


MESSAGE    MSGTXT('Either ADM, FLT or TRVL was entered')

...


WHEN       VALUE_IS('= AUD' '= INF' '= MKT')

...


CHANGE     FIELD(#SECTION) TO(B)

...


MESSAGE    MSGTXT('Either AUD, INF or MKT was entered')

...


OTHERWISE
CHANGE     FIELD(#SECTION) TO(C)

...


MESSAGE    MSGTXT('Other value was entered')

...


ENDCASE

Which is shorter, easier to read, and easier to maintain. 

It also executes faster in most situations.

Anchor
Using the CASE command with OR operations
Using the CASE command with OR operations
Using the CASE command with OR operations

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

...

You could use a CASE command structured like this to implement the required logic:

...

   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  

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

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

...

Hence a CASE command can be used like this:

...

      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 

Anchor
Using the CASE command with compound expressions
Using the CASE command with compound expressions
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:

...

This can be coded by using the CASE command:

...

   DEFINE     FIELD(#DISCOUNT) TYPE(*DEC) LENGTH(11) DECIMALS(2) EDIT_CODE(1)

...


DEFINE     FIELD(#QUANTITY) TYPE(*DEC) LENGTH(3) DECIMALS(0)

...


BEGIN_LOOP

...


REQUEST    FIELDS(#DISCOUNT #QUANTITY)

...


CASE       OF_FIELD(#DISCOUNT)

...


WHEN       VALUE_IS('> (#QUANTITY * 0.1)')

...


MESSAGE    MSGTXT('Discount value is larger than quantity times 0.1')

...


WHEN       VALUE_IS('< (#QUANTITY * 0.1)')

...


MESSAGE    MSGTXT('Discount value is less than quantity times 0.1')

...


OTHERWISE
MESSAGE    MSGTXT('A correct discount value was entered')

...


ENDCASE  
END_LOOP