comp.lang.ada
 help / color / mirror / Atom feed
From: "Matthew Heaney" <matthew_heaney@acm.org>
Subject: Re: GOTO in ADA
Date: 1999/08/14
Date: 1999-08-14T00:00:00+00:00	[thread overview]
Message-ID: <37b62ac7@news1.us.ibm.net> (raw)
In-Reply-To: 6A08418D0DAA75E0.E4D1854DB5A80C85.B4B03E27B8C9304C@lp.airnews. net

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"




       reply	other threads:[~1999-08-14  0:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <6A08418D0DAA75E0.E4D1854DB5A80C85.B4B03E27B8C9304C@lp.airnews. net>
1999-08-14  0:00 ` Matthew Heaney [this message]
1999-08-14  0:00 GOTO in ADA Ray
1999-08-19  0:00 ` Michael F. Yoder
1999-08-22  0:00   ` Robert Dewar
replies disabled

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