COBOL's EVALUATE: How to have an empty WHEN OTHER clause?

Viewed 525
EVALUATE MyValue
WHEN 1
    DISPLAY "My value is 1"
WHEN 2
    DISPLAY "My value is 2"
WHEN OTHER
* Actually I don't need to do anything
END-EVALUATE

I think to have read somewhere that a COBOL application will crash for an EVALUATE where the WHEN clauses do not cover the value of the evaluated variable, if there is no WHEN OTHER statement present. So, in order to avoid the crash, I wanted to add this empty WHEN OTHER.

Under which circumstances is this necessary, and if so, is this approach correct?

3 Answers

Under which circumstances is this necessary ...

Under no circumstances with every COBOL environment I know of (might be "some"); and isn't necessary for any COBOL 85/2002/2014/future compiler.

... and if so, is this approach correct?

No, actually COBOL 85/2002/2014/future-compliant compiler will raise an error because of a missing imperative statement (there are some compilers allowing this as "extension" [I'd say it is a bug], some at least warn, some stay silent).
If there are some "really strange reasons" to always include the WHEN OTHER: use the (nearly no-op) statement CONTINUE as "statement".

Under which circumstances is this necessary

It is definitely necessary if the compiler ist configured to abort if the when other statement ist missing. ;-)

Otherwise, in my opinion it is good practice to programm an when other statement even if it only takes a continue.

Your code should look like this:

EVALUATE MyValue
WHEN 1
    DISPLAY "My value is 1"
WHEN 2
    DISPLAY "My value is 2"
WHEN OTHER
    CONTINUE
END-EVALUATE.
Related