[fpc-pascal] case statement
Tony Whyman
tony.whyman at mccallumwhyman.com
Sun Dec 17 00:00:11 CET 2023
On 16/12/2023 19:07, Gerhard Scholz via fpc-pascal wrote:
>
> ELSE/OTHERWISE
>
> I assume that came historically; the first implementation of a PASCAL
> compiler I have seen had no else or otherwise in the case startement.
> Some ater dialects introduced ELSE, other dialect(s) used OTHERWISE,
> FPC then allowed both.
The historical context has interested me as well.
Going back to the original "Pascal User Manual and Report" (Jensen and
Wirth 1978), there is no Else/Otherwise clause for a case statement. The
syntax is given as (in the original BNF):
<case statement> ::= case <expression> of
<case list element> |;<case list element>] end
<case list element> ::= <case label list> : <statement> | <empty>
<case label list> ::= <case label> | [ , <case label> ]
Note that the semi-colon is used here in its traditional Algol role as a
statement separator and not the 'C' usage as a statement terminator.
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.
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 an aside, I much prefer OTHERWISE to ELSE given that it is much
closer to natural language. The IF...THEN..ELSE construct probably dates
back to Algol 60 and I wonder why it was proposed. In normal English
use, "else" is not used in this way. It usually follows another word,
such as "anyone else", "anything else", "or else".
You might say
"If I go to town then I will be out. Otherwise, I will stay at home". I
would never replace "otherwise" with "else" in such a sentence. "Else"
belongs in a statement such "Does anyone else have a view".
Tony Whyman
More information about the fpc-pascal
mailing list