[comp.lang.pascal] TPascal-puzzle

prknoerr@immd4.informatik.uni-erlangen.de (Peter Knoerrich) (06/25/91)

I once did some hard debugging on some Turbo-Pascal code looking somewhat
like this (wonder if someone is really interested in pascal puzzles !?!?):

var
  confirm : boolean;
  key     : char;

begin
  confirm := false;
  key     := '0';
  case key of
    '1' : Menue1;
    '2' : Menue2;
    '3' : Menue3;
    '0' : if confirm then
            RequestExit;
          else
            FastExit;
  end;
end;

This piece of program would compile OK, but I could never manage to
get a FastExit! To any pascal-fox the solution should be obvious 
(in fact I feel an itch typinng in the error), but at least c-freaks
should have some figuring to do...

-------------------------------------------------------------------------
.sig? You must be kidding with all those SIGKILLs around on this machine!
Peter Knoerrich,        email: prknoerr@faui43.informatik.uni-erlangen.de

bdhelm@immd4.informatik.uni-erlangen.de (Bernd Helm) (06/25/91)

prknoerr@immd4.informatik.uni-erlangen.de (Peter Knoerrich) writes:

>I once did some hard debugging on some Turbo-Pascal code looking somewhat
>like this (wonder if someone is really interested in pascal puzzles !?!?):

>var
>  confirm : boolean;
>  key     : char;

>begin
>  confirm := false;
>  key     := '0';
>  case key of
>    '1' : Menue1;
>    '2' : Menue2;
>    '3' : Menue3;
>    '0' : if confirm then
>            RequestExit;
                        ^ your problem is this semicolon.
                          It finish the if-construct.
                          The 'else' below belongs to 'case key of'.
                          Test it with: key := 'A' or something else
>          else
>            FastExit;
>  end;
>end;


Ciao
	Bernd

fsbrn@BRL.MIL ( Ferd Brundick) (06/25/91)

Haah,

If your sample code is *exactly* what your program used, then the
answer is simple.  You have a semicolon after the THEN clause and that
terminates the IF.  The ELSE belongs to the CASE (as a default or
none-of-the-above entry).  If the Pascal you are using did not allow
an ELSE on a CASE (it is an extension) then the compiler would have
complained about an ELSE without an IF.  IF/THEN/ELSE is a single
complex statement, not a compound statement made up of substatements
that have to be separated by semicolons.

Overall, a ***very*** subtle bug.  I prefer CASEs that use DEFAULT or
OTHERWISE instead of overloading the ELSE verb.

                                        dsw, fferd
                                        Fred S. Brundick
                                        USABRL, APG, MD.
                                        <fsbrn@brl.mil>

pshuang@athena.mit.edu (Ping-Shun Huang) (07/01/91)

In article <27298@adm.brl.mil> fsbrn@BRL.MIL ( Ferd Brundick) writes:

 > Overall, a ***very*** subtle bug.  I prefer CASEs that use DEFAULT or
 > OTHERWISE instead of overloading the ELSE verb.

Quite true, I would also have preferred that -- Modula-2 uses that,
right?  On the other hand, as long as you use an editor (i.e. emacs
pascal-mode) which knows the syntax of the language and can provide the
"correct" indentation, telling the difference between an ELSE which
belongs to the CASE statement or to an IF statement isn't very
difficult.  But then, we're never going to all agree on stylistic
matters like indentation, although Pascal is better than C in that regard.

--
Above text where applicable is (c) Copyleft 1991, all rights deserved by:
UNIX:/etc/ping instantiated (Ping Huang) [INTERNET: pshuang@athena.mit.edu]