[Oberon] How to write this program in Oberon-07?
August Karlstrom
fusionfile at gmail.com
Mon Jan 24 19:29:53 CET 2022
As mentioned by Jörg, Oberon-07 is a pure structured language in the
sense that each (ASSERT free) statement sequence is either fully
executed or not executed. With CASE statements you can often limit the
range of values to inspect before the CASE statement is executed. Here
is one way to do it:
MODULE case;
IMPORT In, Out;
VAR
countVowels: INTEGER;
ch, capCh: CHAR;
BEGIN
In.Open;
countVowels := 0;
In.Char(ch);
WHILE In.Done DO
IF (ch >= "a") & (ch <= "z") THEN
capCh := CHR(ORD("A") + ORD(ch) - ORD("a"))
ELSE
capCh := ch
END;
IF (capCh >= "A") & (capCh <= "U") THEN
CASE capCh OF
"A", "E","I","O","U":
INC(countVowels) |
"B", "C", "D", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q",
"R", "S", "T":
(*do nothing*)
END
END;
In.Char(ch)
END;
Out.Int(countVowels, 0);
Out.String(" vowels read.");
Out.Ln
END case.
With the WHILE loop we can see instantly that it will continue until
there are no more characters to read. Compare this to the LOOP in which
the exit condition (or possibly exit conditions) is buried in the
contained statement sequence.
-- August
On 2022-01-24 15:14, Joe Turner wrote:
> I'm working through the book "Into the Realm of Oberon" which is written
> for Oberon-2. I can't figure out how to translate the following program
> into Oberon-07 (I'm using the OBNC compiler):
>
> MODULE case;
> IMPORT In,Out;
>
> VAR countVowels: INTEGER; ch: CHAR;
>
> BEGIN
> In.Open;
> countVowels:=0;
> LOOP
> In.Char(ch);
> IF ~In.Done THEN EXIT END;
> CASE ch OF
> "a", "e", "i", "o", "u",
> "A", "E","I","O","U": INC(countVowels)
> ELSE
> END;
> END;
> Out.Int(countVowels,0);
> Out.String(" vowels read.");Out.Ln;
> END case.
>
> Oberon-07 doesn't allow LOOP, EXIT, or ELSE in CASE.
> Thanks in advance for any help.
> -- Sent with https://mailfence.com Secure and private email
>
> --
> Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related systems
> https://lists.inf.ethz.ch/mailman/listinfo/oberon
More information about the Oberon
mailing list