7.11.3 CASE Examples
Using the CASE command with OR operations
Using the CASE command with operations other than "=" (equal to)
Using the CASE command with compound expressions
Basic CASE Processing
This example illustrates the most basic type of CASE command processing.
Imagine that a user inputs a value into field #DEPTMENT and that a CASE command to be used to implement this logic:
When user inputs this value into field #DEPTMENT | Field #SECTION should be changed to this value . . . | And this message should be issued . . . . |
|---|---|---|
ADM | A | ADM was entered |
AUD | B | AUD was entered |
FLT | C | FLT was entered |
TRVL | D | TRVL was entered |
Any other value | E | Other value was entered |
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
Why use the CASE command?
Imagine you have to implement this logic:
When field #DEPTMENT is this value ... | Field #SECTION should be changed to this value ... | And this message should be issued .... |
|---|---|---|
ADM or FLT or TRVL | A | Either ADM , FLT or TRVL was entered |
AUD or INF or MKT | B | Either AUD, INF or MKT was entered |
Any other value | C | Other value was entered |
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
ENDIFHowever, 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.
Using the CASE command with OR operations
Imagine that when field #TEST contains A or B or C you have to perform some operation.
If it contains X or Y or Z you have to perform some other operation.
If it contains any other value then you have to perform yet another 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
Using the CASE command with operations other than "=" (equal to)
Imagine that when field #COPIES needs to be validated according to these rules:
Value of #COPIES | Message to Issue |
|---|---|
= 0 | Value of zero is invalid |
< 0 | Value is negative ! |
1 to 50 | Value is sensible |
> 50 | Value is probably incorrect |
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
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:
When user inputs this value into fields #DISCOUNT, #QUANTITY | This equation is used to validate the discount value | A message will be displayed |
|---|---|---|
1000, 20 | #QUANTITY * 0.1 | Discount value is larger than QUANTITY times 0.1 |
10, 200 | #QUANTITY * 0.1 | Discount value is less than QUANTITY times 0.1 |
10, 100 | #QUANTITY * 0.1 | A correct discount value was entered |
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