2

Does

CASE <var/expr> OF
      1: <do statement>;
      2: <do statement>;
   3..5: <do statement>;
END;

Always effectively mean:

(in) CASE <var/expr> EQUALS
      1: <do statement>;
      2: <do statement>;
   3..5: <do statement>;
     ^ value/char in range
END;

Translated to natural language? I'm just wondering why this wording choice was made. Otherwise Pascal syntax seems to read so naturally, grammatically. But maybe I understand the 'case statement' incorrectly?

EDIT: added range case, and parenthesis around (in) as it confused people.

Gaai
  • 98
  • 1
  • 8
  • It's most likely a compromise, since all those `value:` could also be understood as labels (as in `goto label`) and the term "jump" would fit even less. – AmigoJack Oct 24 '22 at 17:29
  • @AmigoJack Well, a [jump table](https://en.wikipedia.org/wiki/Jump_table) _could_ be used to optimize a `case` statement, so the term “jump” could be _technically_ accurate, but this is evidently an _implementation_ detail. – Kai Burghardt Oct 24 '22 at 17:57
  • Please note that your suggested alternative is awkward because (1) `in case` is two tokens, not one and (2) the `case` statement works not only with values, but with ranges and `in case x equals 0: DoThis; 1..9: DoThat; end` is not natural. So maybe the actual syntax is the best one possible? (Well, not only ranges, but also `1..9, 16, 20..22`.) – Andreas Rejbrand Oct 24 '22 at 19:22
  • I only added 'in', to make a sentence 'in the natural language translation'. Of course I would not suggest to add it, as an alternative. – Gaai Oct 24 '22 at 19:30
  • As for the range case. I myself would read that as 'in Case Equals a value in Range', internally. So it still feels more natural to me ;) – Gaai Oct 24 '22 at 19:38
  • What does the documentation say? – David Heffernan Oct 24 '22 at 20:03

1 Answers1

6

It could be read like "In case var is of value1 do_this, in case var is of value2 do_that...". So it indeed means equal.

A statement like

case v of
  value1: do_this;
  value2: do_that;
else
  do_something_else;
end;

boils down to

if v = value1 then
  do_this
else if v = value2 then
  do_that
else
  do_something_else;
Uwe Raabe
  • 45,288
  • 3
  • 82
  • 130
  • 2
    I would emphasize that this is a _semantic_ equivalent. In the generated code, `v` is not evaluated `n` times, nor are there necessarily `n` comparisons. – Kai Burghardt Oct 24 '22 at 15:43
  • 4
    Since `value` also can represent a range, `equals/within` seems a reasonable interpretation. – LU RD Oct 24 '22 at 15:48
  • Thanks for taking the time to answer / comment. Glad I didn't interpret the case statement wrong in Pascal. I'm still wondering why the developer chose this wording. Perhaps he thought of using 'equals' somewhere else in the language, to replace '=' sign for comparisons for example. Or indeed it's short for 'is of value'. It's shorter to type also. But that didn't bother him elsewhere. ;) – Gaai Oct 24 '22 at 18:37
  • 2
    "in case of emergency" => "case situation of emergency: call911" – Andreas Rejbrand Oct 24 '22 at 19:17
  • @AndreasRejbrand at first I thought you where joking but actually that is a natural way of reading it yes. In case of fire: call 911; snow: make snowman; flood: take boat END; – Gaai Oct 24 '22 at 20:58