comp.lang.ada
 help / color / mirror / Atom feed
* Re: GOTO in ADA
       [not found] <6A08418D0DAA75E0.E4D1854DB5A80C85.B4B03E27B8C9304C@lp.airnews. net>
@ 1999-08-14  0:00 ` Matthew Heaney
  0 siblings, 0 replies; 4+ messages in thread
From: Matthew Heaney @ 1999-08-14  0:00 UTC (permalink / raw)


In article 
<6A08418D0DAA75E0.E4D1854DB5A80C85.B4B03E27B8C9304C@lp.airnews.net> , Ray
<crwhite2@airmail6.net>  wrote:

> I know that the use of GOTO statements are anathema to most diehard
> structured programmers, but can someone give an example of  a valid use
> of the GOTO in "real life" code?

Typically, you use a goto statement when implementing a state-driven
abstraction.  I do this all the time when using tasks.  For example:

  task body T is
  begin
    select
      accept Initialize do
        <init>
      end;

    or
      accept Finalize;
      goto Terminate_Task;

    end select;

    <<Main>> null;

    <lots of entries and stuff>

    ...
       accept Finalize;
       goto Terminate_Task;
    ...

    goto Main;

    <<Terminate_Task>> null;

  end T;


Another classic example is an FSM, say a parser or scanner.  You use a goto
statement to easily navigate among the states.

This question comes up a lot on comp.lang.ada.  Search DejaNews for other
threads.

In general, you should feel free to use a goto.  As Robert Dewar pointed
out:

(start of message)
From: dewar@merv.cs.nyu.edu (Robert Dewar)
Subject: Re: State Machine Implementation
Date: 06 Aug 1998 00:00:00 GMT
Message-ID: <dewar.902413526@merv>
References: <35C90369.CFE8ED98@inficad.com>
X-Complaints-To: usenet@news.nyu.edu
X-Trace: news.nyu.edu 902413682 27275 (None) 128.122.140.58
Organization: New York University
Newsgroups: comp.lang.ada

<<Does anyone have any implementions of a state machine?  The states would
be just an enumerated type and I am guessing you would use a case
statement.  Just looking for more info on how to implement this using
Ada.>>

A case statement is possible certainly, and it is hard to know exactly what
you mean by "an implementation", since this is the sort of thing that occurs
all the time in a programs, and the application dependent details are 95%
of the code anyway.

Note that an alternative, probably more efficient, and for many people more
readdable way of coding a finite state machine in languages like Ada is
to use a label on each state, and goto's to effect state transition, thus
efficiently encoding the state in the program counter.

Some people have this silly allergy to goto's (a case of understanding an
important principle without really understanding it), but in fact this is
for many programmers one of the very few legitimate uses of gotos.
(end of message)

--
It is impossible to feel great confidence in a negative theory which has
always rested its main support on the weak points of its opponent.

Joseph Needham, "A Mechanistic Criticism of Vitalism"




^ permalink raw reply	[flat|nested] 4+ messages in thread

* GOTO in ADA
@ 1999-08-14  0:00 Ray
  1999-08-19  0:00 ` Michael F. Yoder
  0 siblings, 1 reply; 4+ messages in thread
From: Ray @ 1999-08-14  0:00 UTC (permalink / raw)


I know that the use of GOTO statements are anathema to most diehard
structured programmers, but can someone give an example of  a valid use
of the GOTO in "real life" code?

--
Remove the numbers from my address to reply.

Ray






^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: GOTO in ADA
  1999-08-14  0:00 Ray
@ 1999-08-19  0:00 ` Michael F. Yoder
  1999-08-22  0:00   ` Robert Dewar
  0 siblings, 1 reply; 4+ messages in thread
From: Michael F. Yoder @ 1999-08-19  0:00 UTC (permalink / raw)


Ray wrote:
> 
> I know that the use of GOTO statements are anathema to most diehard
> structured programmers, but can someone give an example of  a valid use
> of the GOTO in "real life" code?

If you are writing a dialog which asks many questions, and want to
re-ask one (e.g. because of an error), "goto Ask_Password;" is IMO
clearer than writing "loop... exit when Password_OK; end loop;" or
whatever.  This is particularly so in cases where a straightforward use
of loops would make them overlap, for example when the flow of control
looks like this:

      <ask Q1>
      ...
      <ask Q2>
      ...
      <if error, return to ask Q1>
      ...
      <if error, return to ask Q2>

-- 
Michael F. Yoder

Unscientific man is beset by a deplorable desire to have been right.
The scientist is distinguished by a desire to *be* right. -- W.V. Quine




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: GOTO in ADA
  1999-08-19  0:00 ` Michael F. Yoder
@ 1999-08-22  0:00   ` Robert Dewar
  0 siblings, 0 replies; 4+ messages in thread
From: Robert Dewar @ 1999-08-22  0:00 UTC (permalink / raw)


In article <37BC2E9E.4D4EF69C@decada.zko.dec.com>,
  "Michael F. Yoder" <yoder@decada.zko.dec.com> wrote:
> If you are writing a dialog which asks many questions, and
want to
> re-ask one (e.g. because of an error), "goto Ask_Password;" is
IMO
> clearer than writing "loop... exit when Password_OK; end
loop;" or
> whatever.  This is particularly so in cases where a
straightforward use
> of loops would make them overlap, for example when the flow of
control
> looks like this:

And now those who have not killed this thread (I am not among
their number, it's certainly in my kill file now) will have to
hope that this does not lead to yet another of those off-topic
long and boring rants and raves about when gotos are or are
not justified :-)


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1999-08-22  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <6A08418D0DAA75E0.E4D1854DB5A80C85.B4B03E27B8C9304C@lp.airnews. net>
1999-08-14  0:00 ` GOTO in ADA Matthew Heaney
1999-08-14  0:00 Ray
1999-08-19  0:00 ` Michael F. Yoder
1999-08-22  0:00   ` Robert Dewar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox