Case Structure

The CASE structure is an alternate way to check conditions and direct processing according to the results of the condition. The EVALUATE does just that, it evaluates something, the contents of a field, a literal or a c ondition and then selects from a series of options which action it will take. First, let's examine some examples.

EXAMPLE #1:

	      EVALUATE  TYPE-EMPLOYEE
	          WHEN "F" 
			MOVE "FULL TIME" TO EMP-TYPE-PR
	          WHEN "P" 
			MOVE "PART TIME" TO EMP-TYPE-PR
	          WHEN "C" 
			MOVE "CONSULTANT" TO EMP-TYPE-PR
	          WHEN "T" 
			MOVE "TEMPORARY" TO EMP-TYPE-PR
                  WHEN OTHER
			MOVE "INVALID" TO EMP-TYPE-PR.

In this statement, the field TYPE-EMPLOYEE is evaluated, and depending on the code in the field a move is executed. Only one WHEN will be executed. For example, if the code is equal to P the message PART TIME is moved to EMP-TYPE-PR and the evaluate is complete. Control passes to the next statement. If the code is not equal to either F or P or C or T then the OTHER option is taken. OTHER is a reserved word that means if no other option is taken, do this.

If this had been coded using an IF, the statement would read like this:

	IF TYPE-EMPLOYEE = "F" 
            MOVE "FULL TIME" TO EMP-TYPE-PR
	ELSE
            IF TYPE-EMPLOYEE = "P"
                MOVE "PART TIME" TO EMP-TYPE-PR
	    ELSE	 
		IF TYPE-EMPLOYEE = "C"
                    MOVE "CONSULTANT" TO EMP-TYPE-PR
                ELSE
                    IF TYPE-EMPLOYEE = "T"
                        MOVE "TEMPORARY" TO EMP-TYPE-PR
                    ELSE
                        MOVE "INVALID" TO EMP-TYPE-PR.

Some programmers code the EVALUATE using this style. Depending on what is being tested and your personal taste either way is acceptable.
	EVALUATE  TYPE-EMPLOYEE
	          WHEN "F"     MOVE "FULL TIME" TO EMP-TYPE-PR
	          WHEN "P"     MOVE "PART TIME" TO EMP-TYPE-PR
	          WHEN "C"     MOVE "CONSULTANT" TO EMP-TYPE-PR
	          WHEN "T"     MOVE "TEMPORARY" TO EMP-TYPE-PR
                  WHEN OTHER   MOVE "INVALID" TO EMP-TYPE-PR.

EXAMPLE #2:

	EVALUATE ON-HAND
	   WHEN 0
	       PERFORM B-310-NO-INVENTORY
	   WHEN 1 THRU 100
               PERFORM B-320-INVENTORY-LOW
	   WHEN 101 THRU 500
               PERFORM B-330-INVENTORY-NORMAL
           WHEN OTHER
               PERFORM B-340-INVENTORY-HIGH
	END-EVALUATE.

NOTE: The END-EVALUATE is an optional statement similiar to the END-IF. If it is not there, the EVALUATE will terminate with the period. The THRU statement is used to check for the range of 1 through 100 and the range of 101 thru 500, these numbers are inclusive that is 1 through 100 includes both 1 and 100.

Another way to code this example uses the reserved word TRUE. When EVALUATE TRUE is written, it means if the condition in each WHEN statement is TRUE then the command following the condition should be executed. There is also a reserved word FALSE which would check to see if the condition evaluated FALSE. When using EVALUATE, you can have the WHEN clause evaluate a level 88 name as the condition instead of writing out the condition.

	EVALUATE TRUE
	    WHEN ON-HAND = 0
	        PERFORM B-310-NO-INVENTORY
	    WHEN  ON-HAND > 0 AND  <  101
		PERFORM B-320-INVENTORY-LOW
            WHEN ON-HAND > 101 AND < 501
                PERFORM B-330-INVENTORY-NORMAL
            WHEN OTHER
                PERFORM B-340-INVENTORY-HIGH
	END-EVALUATE.

The IF statement for this example would be:

	IF ON-HAND = 0
	    PERFORM B-310-NO-INVENTORY
	ELSE
	    IF ON-HAND > 0 AND < 101
		PERFORM B-320-INVENTORY-LOW
            ELSE
                IF ON-HAND > 100 AND < 501
	            PERFORM B-330-INVENTORY-NORMAL
	        ELSE
		    PERFORM B-340-INVENTORY-HIGH.

EXAMPLE #3:

The evaluate can also be used to accomplish the same thing that a simple IF statement would accomplish:

	EVALUATE ON-ORDER >= 1000
	    WHEN TRUE PERFORM B-310-CHECK-ONORDER.

Or a second way of coding the above, would be:

	EVALUATE ON-ORDER
            WHEN 0 THRU 1000 PERFORM B-310-CHECK-ONORDER.

SYNTAX OF simple EVALUATE:

          {identifier}       {condition       }
          {literal   }	     {TRUE            }                   
EVALUATE  {expression}  WHEN {FALSE           }
          {TRUE      }       {[NOT]{identifier}      {identifier}]
          {FALSE     }             {literal   }[THRU {literal   }]
                                   {expression}      {expression}]

                 imperative statement

[WHEN OTHER  imperative statement]

[END-EVALUATE]


Remember:
{ means one possible option
[ means this entry is optional