[fpc-pascal] case statement

Adriaan van Os adriaan at adriaan.biz
Sun Dec 17 16:36:05 CET 2023


Tony Whyman via fpc-pascal wrote:

> Back in the early eighties, I worked at ICL and we made extensive use of 
> the Prospero Pascal compiler building embedded systems based on the Z80 
> microprocessor. I still have a 1988 edition of the language 
> specification, and this uses EBNF to define the case statement as:
> 
> case-statement = "CASE" case-index "OF"
>   case-list-element {";" case-list-element }
>    [ ";" OTHERWISE statement][";"] "END"
> 
> case-index = expression
> case-list-element = case-range-list ":" statement
> case-range-list = case-range {"," case-range }
> case-range = case-constant [".." case-constant ]
> case-constant = constant
> 
> What is interesting about the above is not just that it uses "otherwise" 
> but that it insists on a semi-colon before the "otherwise". This may not 
> have been strictly necessary but it does enforce the separation between 
> the case -list-elements and the "otherwise", making the "otherwise" 
> clause into another case-list-element. It also aids readability and it 
> may be why I have always added a semi-colon after the final 
> case-list-element. Note that the final optional ";" is probably needed 
> to allow those that always like to end a statement in a semi-colon.
> 
> Prospero Pascal was close to ISO Pascal (although I have lost my 
> original copy of ISO Pascal) and I would guess that the above is copied 
> from ISO Pascal.

ISO-7185 Pascal doesn't have an else/otherwise in the case-statement, ISO-10206 Extended Pascal 
does, but it is of later date.

> The change from "otherwise" to "else" is probably a Borland Pascal invention preferring a shorter word and then overlooking the importance of the semi-colon case-list-element separator and the resulting ambiguity. The same error then flowed through to exception handling.

As the otherwise-clause is not in ISO-7185 Pascal, it seems more plausible that Borland invented 
the else-clause (without semicolon) independently. All other Pascals I have looked at, use an 
otherwise-clause (with an obligatory semicolon). The motivation for this, given in IBM Pascal is 
interesting. The manual says that the statement-part of the otherwise-clause can be intentionally 
"left blank" and be used "to prevent possible errors during execution". I recall that in ISO-7185 
Pascal it is an error if no case discriminator matches at runtime. So, the otherwise-clause was 
seen as a way to get around that !

Regards,

Adriaan van Os


More information about the fpc-pascal mailing list