CASE Statement

orjazmik

New Member
I'm writing a program to assign a value to a field in the customer records based on some calculated sales dollars. There are multiple values that this field can be assigned to and this is driven by where the total sales value falls. I thought the best way to execute this would be by using the CASE statment... but I'm not sure how to say WHEN the value of this variable is between this value and this value, then assign the field to this. I thought this might be cleaner than a bunch of IF THEN statements, but I'm having trouble figuring out the syntax. Is this even a possibility to make work? Or is there another way I should be approaching this? Thanks in advance for your help.

- Marshall
 
CASE checks for a specific value. To the best of my knowledge you cannot test for a range. Looks like you are stuck with the IF... THEN... ELSE...

The only way you could use CASE would be by assigning a value to the range:

Code:
IF     field-to-test >= 1 
AND field-to-test <= 99 THEN
  ASSIGN
    range = 'A'.

IF     field-to-test >= 100
AND field-to-test <= 499 THEN
  ASSIGN
     range = 'B' /* etc */

CASE range
  WHEN 'A' THEN
  DO:
  /* Appropriate processing */
  END.

  WHEN 'B' THEN
  DO:
  /* Appropriate processing */
  END.

But IMHO there is nothing to be gained from this but extra overhead (Unless you have a particular need to identify the range by a separate means).
 
That does make sense. I will just stick with the if, then statements then. I appreciate the insight. Thanks a lot. :-)
 
One thing that I do that I ASSUME minimally reduces the overhead is to put all of the if/then statments to the right of the equal sign.
such as

v-fielda = if (v-test-field > 0 and v-test-field <= 100)
then v-valuea
else (if v-test-field > 100 and v-test-field <= 200)
then v-valueb
else v-valuec.

Naturally you can else/if/then more times than this example but you must end in a final 'else' statement or the syntax pukes.

Since this is one statement I figure the overhead is less than;

if v-test-field > 0 and v-test-field <= 100
then v-fielda = v-valuea.

if v-test-field > 100 and v-test-field <= 200
then v-fielda = v-valueb.

if v-test-field >200
then v-fielda = v-valuec.

Chris, would you happen to know which, if either, of these examples has a lower overhead? Or does it just come down to the programmer doing what (s)he likes to do best?

Mark
 
I honestly don't know which would have the highest overhead, I would imagine however that there would not be a vast amount of difference and unless you were dealing with a large number of iterations it wouldn't make too much real difference. It would be interesting at some point to do a few timing tests on it.
 
Back
Top