Board index » delphi » Case Statement

Case Statement

I just started learning Pascal.

I wrote a simple program that uses a case statement (the cases are
integers) to branch off to three separate procedures.  I used an if-then
statement around it (as suggested by my book), to ensure that only the
integers (1,2,3, and 4) would be processed.  It works, except that if
any char's --- or other keys --- are pressed, I get an error.   Now, my
book doesn't explain what to do about this.  I spent quite a bit of time
trying different things, with no luck.  How do I make sure only the
specified integers will be processed.

(Prior to this program, I wrote the equivalent program just using
if-then statements.  No problems with it, but I presumed that it would
be easier (and more readable with a case statement.)

 

Re:Case Statement


use 'else' as a choice - if it isn't '1' '2' or '3' then else catches it
{ the non goto people in this newsgropup will probably supply other
code  that does the same thing }

program test;
uses dos, crt;
var ch : char;

begin
1:
ch := readkey;
case ch of
 '1' : pgm1;
 '2' : pgm2;
 '3' : pgm3;
 else : goto 1;
end;

{ after pgm1, pgm2, pgm3 end they all execute the line here }
end.

Quote
Jim Roberts wrote:

> I just started learning Pascal.

> I wrote a simple program that uses a case statement (the cases are
> integers) to branch off to three separate procedures.  I used an if-then
> statement around it (as suggested by my book), to ensure that only the
> integers (1,2,3, and 4) would be processed.  It works, except that if
> any char's --- or other keys --- are pressed, I get an error.   Now, my
> book doesn't explain what to do about this.  I spent quite a bit of time
> trying different things, with no luck.  How do I make sure only the
> specified integers will be processed.

> (Prior to this program, I wrote the equivalent program just using
> if-then statements.  No problems with it, but I presumed that it would
> be easier (and more readable with a case statement.)

Re:Case Statement


Quote
enztec <enz...@xmission.com> wrote:
>{ the non goto people in this newsgropup will probably supply other
>code  that does the same thing }

I'd write it as:

repeat
 ch := readkey;
until ch in [ '1' .. '3'];

case ch of
 '1' : pgm1;
 '2' : pgm2;
 '3' : pgm3;
end;

Jud McCranie

Re:Case Statement


In article <37ED3D3B.67D02...@xmission.com>,

Quote
enztec  <enz...@xmission.com> wrote:
>use 'else' as a choice - if it isn't '1' '2' or '3' then else catches it
>{ the non goto people in this newsgropup will probably supply other
>code  that does the same thing }

>program test;
>uses dos, crt;
>var ch : char;

>begin
>1:
>ch := readkey;
>case ch of
> '1' : pgm1;
> '2' : pgm2;
> '3' : pgm3;
> else : goto 1;
>end;

That is just terrible. While there are cases where goto is usable one
should never goto backwards, i.e. create loops with goto. Also you can
use symbolic labels in TP. However, one must define each label before it
is used.

I just do not understand where you even got the idea to use goto in such
a case.

Osmo

Re:Case Statement


osmo, you cute little code fascist - thanks for pointing out my only
error. Yes, the label must be declared.

Did I miss the code you posted?? I'll bet your code will be mucho more
efficient than mine.

program test;
uses dos, crt;
label 1;            { label must be declared }
var ch : char;

begin
1:
ch := readkey;
case ch of
 '1' : pgm1;
 '2' : pgm2;
 '3' : pgm3;
 else : goto 1;
end;

{ after pgm1, pgm2, pgm3 end they all execute the line here }

end.

end.

Re:Case Statement


Very instructive (and liveley thread).   But I cant't find the message(s)
that is the reference for the quotes below.  I looked and looked.
Quote

> > I did not post code as Jud McCranie posted a good solution.

> >  Efficacy is not the issue in such a code, readability is.

> > The proper structure is:

> > Now clearly this is more readable than the goto version.

Re:Case Statement


There is no post from Jud McCranie in this thread - I don't know where
you got this post
Quote
Pedt Scragg wrote:

> enztec <mailto:enz...@xmission.com> said:

> >Jim Roberts wrote:

> >> Very instructive (and liveley thread).   But I cant't find the message(s)
> >> that is the reference for the quotes below.  I looked and looked.

> >> > > I did not post code as Jud McCranie posted a good solution.

> >> > >  Efficacy is not the issue in such a code, readability is.

> >there isn't any

> Then enztec, you have not been following the thread:

> [quote from Message-ID: <xEntNw4qL=fJxVpba4MUl1pEZ...@4ax.com>]
> enztec <enz...@xmission.com> wrote:

> >{ the non goto people in this newsgropup will probably supply other
> >code  that does the same thing }

> I'd write it as:

> repeat
>  ch := readkey;
> until ch in [ '1' .. '3'];

> case ch of
>  '1' : pgm1;
>  '2' : pgm2;
>  '3' : pgm3;
> end;

> Jud McCranie

> [end quote]

> Hope that helps you Jim.
> --
> Information on Newsgroup posted weekly on Sunday - read before writing!
> Contains links to    |  http://homepages.force9.net/pascal/faq/
> helpful information  |  http://www.merlyn.demon.co.uk/clpb-faq.txt
> and some guidelines  |  ftp://garbo.uwasa.fi/pc/doc-net/faqclpb.zip

Re:Case Statement


if you think this code is cleaner than my code then you are just being
difficult.

program test;
uses dos, crt;
label 1;            { label must be declared }
var ch : char;

begin
1:
ch := readkey;
case ch of
 chr(0) : begin
ch := readkey;
WriteLn('You''ve pressed a function key! Well blow-me-down!');
end;
 '1' : pgm1;
 '2' : pgm2;
 '3' : pgm3;
 else begin
WriteLn('Hey! Einstein! I wanted NUMBERS!');
goto 1;
end;
end;

{ after pgm1, pgm2, pgm3 end they all execute the line here }

end.

you have extra logic that is uncessary. quit := false - what an
uncessary work around.

your repeat/until :

repeat
writeln('is a goto');
until false;

Quote
Stewart Honsberger wrote:

> On Sun, 26 Sep 1999 19:20:19 -0600, enztec wrote:
> >your code is starting to look a little messy now.
> >now what do you have to put into this code if the ch is not in '1..3'??
> >and add code to handle function key.

> My my my.. How complicated;

> Repeat
>   ch:=readkey;
>   quit:=true;
>   case ch of
>     #0 : begin
>       ch := ReadKey;
>       WriteLn('You''ve pressed a function key! Well blow-me-down!');
>     end;
>     '1': {...};
>     '2': {...};
>     '3': {...};
>     'A'..'Z': WriteLn('Hey! Einstein! I wanted NUMBERS!');
>     else quit:=false;
>   end;
> until quit;

> The snippet of code that you posted looked much "messier" than his code.

> IMHO - this code is a lot easier to follow. There are dozens of reasons
> not to use goto, especially in a rapidly growing program. Following begin..
> end's is a lot easier than trying to find all the labels that correspond to
> goto's.

> Now, lets all put our {*word*62} hats on and try to have a civilized discussion.

> --
> Stewart Honsberger (AKA Blackdeath) @ http://sprk.com/blackdeath/
> blackde...@13softhome.net  (Remove 'thir{*word*249}' to reply privately)
> Humming along under SuSE Linux 6.0 / OS/2 Warp 4

Re:Case Statement


Quote
enztec wrote:
> There is no post from Jud McCranie in this thread - I don't know where
> you got this post

I apologize for the poor formatting of this, but this is what I "cut and pasted"
from my
newsreader.

-------------------

Subject:
             Re: Case Statement
        Date:
             Sat, 25 Sep 1999 18:17:41 -0400
       From:
             Jud McCranie <jud.mccra...@mindspring.com>
    Reply-To:
             jud.mccranie(at)mindspring.com
 Organization:
             MindSpring Enterprises
 Newsgroups:
             comp.lang.pascal.borland
  References:
             1 , 2

Quote
enztec <enz...@xmission.com> wrote:
>{ the non goto people in this newsgropup will probably supply other
>code  that does the same thing }

I'd write it as:

repeat
 ch := readkey;
until ch in [ '1' .. '3'];

case ch of
 '1' : pgm1;
 '2' : pgm2;
 '3' : pgm3;
end;

Jud McCranie

Re:Case Statement


In article <37EEA073.59B8A...@xmission.com>,

Quote
enztec  <enz...@xmission.com> wrote:
>"little boys - little boys - you are all just little boys"

>your code is a duplication in effort - the repeat-until - the ch is in
>the set { '1' .. '3'} is exactly what the case statement tests for - a
>test for set inclusion. I would think this wastes the 'power' of having
>a case statement to begin with.

Are you kidding! The issue is to produce readable and working code, not
to think whether one wastes the power of statements.

Sure one could use also just case:

Repeat
   ch:=readkey;
   quit:=true;
   case ch of
         '1':...
         '2':...
         '3':...
         else quit:=false;
  End;
until quit;

I just do not think that is nice.

Quote

>If you want to add another choise - '4' you have to edit two lines of
>code rather than one.

So?

Osmo

Re:Case Statement


enztec <mailto:enz...@xmission.com> said:

Quote
>Jim Roberts wrote:

>> Very instructive (and liveley thread).   But I cant't find the message(s)
>> that is the reference for the quotes below.  I looked and looked.

>> > > I did not post code as Jud McCranie posted a good solution.

>> > >  Efficacy is not the issue in such a code, readability is.

>there isn't any

Then enztec, you have not been following the thread:

[quote from Message-ID: <xEntNw4qL=fJxVpba4MUl1pEZ...@4ax.com>]

Quote
enztec <enz...@xmission.com> wrote:
>{ the non goto people in this newsgropup will probably supply other
>code  that does the same thing }

I'd write it as:

repeat
 ch := readkey;
until ch in [ '1' .. '3'];

case ch of
 '1' : pgm1;
 '2' : pgm2;
 '3' : pgm3;
end;

Jud McCranie

[end quote]

Hope that helps you Jim.
--
Information on Newsgroup posted weekly on Sunday - read before writing!
Contains links to    |  http://homepages.force9.net/pascal/faq/
helpful information  |  http://www.merlyn.demon.co.uk/clpb-faq.txt
and some guidelines  |  ftp://garbo.uwasa.fi/pc/doc-net/faqclpb.zip

Re:Case Statement


Hi,

on Sun, 26 Sep 1999 at 23:53:03 o'clock, Jim Roberts wrote:

Quote
> But I cant't find the message(s)
> that is the reference for the quotes below.  I looked and looked.

The Message-Id is <xEntNw4qL=fJxVpba4MUl1pEZ...@4ax.com>. The solution
is almost identical to the one posted by Osmo Ronkanen.

 - Sebastian

--
Proud member of the Pascal Signature Ring!

<< previous | random | next >>

Re:Case Statement


Quote
On Sun, 26 Sep 1999 19:20:19 -0600, enztec wrote:
>your code is starting to look a little messy now.
>now what do you have to put into this code if the ch is not in '1..3'??
>and add code to handle function key.

My my my.. How complicated;

Repeat
  ch:=readkey;
  quit:=true;
  case ch of
    #0 : begin
      ch := ReadKey;
      WriteLn('You''ve pressed a function key! Well blow-me-down!');
    end;
    '1': {...};
    '2': {...};
    '3': {...};
    'A'..'Z': WriteLn('Hey! Einstein! I wanted NUMBERS!');
    else quit:=false;
  end;
until quit;

The snippet of code that you posted looked much "messier" than his code.

IMHO - this code is a lot easier to follow. There are dozens of reasons
not to use goto, especially in a rapidly growing program. Following begin..
end's is a lot easier than trying to find all the labels that correspond to
goto's.

Now, lets all put our {*word*62} hats on and try to have a civilized discussion.

--
Stewart Honsberger (AKA Blackdeath) @ http://sprk.com/blackdeath/
blackde...@13softhome.net  (Remove 'thir{*word*249}' to reply privately)
Humming along under SuSE Linux 6.0 / OS/2 Warp 4

Re:Case Statement


enztec <mailto:enz...@xmission.com> said:

Quote
>Pedt Scragg wrote:

>> enztec <mailto:enz...@xmission.com> said:

>> Then enztec, you have not been following the thread:

>There is no post from Jud McCranie in this thread - I don't know where
>you got this post
>> [quote from Message-ID: <xEntNw4qL=fJxVpba4MUl1pEZ...@4ax.com>]

Look up that Message-ID: at http://www.deja.com/
--
Information on Newsgroup posted weekly on Sunday - read before writing!
Contains links to    |  http://homepages.force9.net/pascal/faq/
helpful information  |  http://www.merlyn.demon.co.uk/clpb-faq.txt
and some guidelines  |  ftp://garbo.uwasa.fi/pc/doc-net/faqclpb.zip

Re:Case Statement


In article <37EED4BE.98243...@xmission.com>,

Quote
enztec  <enz...@xmission.com> wrote:
>if you think this code is cleaner than my code then you are just being
>difficult.

No, he just has learned to read structured code. I have programmed with
Basic and when I first heard that one should not use Goto I thought it
was ludicrous. I know better now. Goto, especially backwards Goto is
ugly.

Also as you do not even indent the code I have to conclude that you are
as troll.

Osmo

Go to page: [1] [2]

Other Threads