comp.lang.ada
 help / color / mirror / Atom feed
From: John G. Volan <jvolan@ti.com>
Subject: Re: Is there an ADA analogue to the C++ continue statement?
Date: 1997/09/29
Date: 1997-09-29T00:00:00+00:00	[thread overview]
Message-ID: <875552739.30613@dejanews.com> (raw)
In-Reply-To: dewar.874665174@merv

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 4917 bytes --]


Robert Dewar wrote:
>
> The most reasonable translation [of continue]
> is to introduce a very stylized use of
> the goto for this purpose, putting a label <<CONTINUE>> just before the
> end loop, and then doing a goto to get the effect you want. So we would
> have:
>
>     while Condition_1 loop
>        statement_1;
>
>        if Condition_2 then
>           goto Continue;
>        end if;
>
>        statement_3;
>
>     <<Continue>>
>     end loop;

One difficulty with this: If you have more than one loop within the same
scope which needs to work this way, then you will encounter a name clash
unless you come up with a different label-name for each loop, e.g.:

    while Condition_1 loop
       statement_1;

       if Condition_2 then
          goto Continue_1;
       end if;

       statement_3;

       <<Continue_1>>
          null;
          -- note: a goto-label has to have a statement to hang on
    end loop;

    while Condition_3 loop
       statement_4;

       if Condition_4 then
          goto Continue_2;
       end if;

       statement_5;

       <<Continue_2>>
          null;
          -- note: a goto-label has to have a statement to hang on
    end loop;

This might be a nuissance for some people, just as it would be a
nuissance if we were still living in the age of assembly, where even
simple if-statements required uniquely-named labels.

Then again this could have been worse: If Ada�s rules regarding goto
were more liberal, the practice Robert suggests could have been a source
of bizarre bugs that might have arisen from simple typographical errors:

    while Condition_1 loop
       statement_1;

       if Condition_2 then
          goto Continue_1;
       end if;

       statement_3;

       <<Continue_1>> null;
    end loop;

    while Condition_3 loop
       statement_4;

       if Condition_4 then
          goto Continue_1; -- Oops!
       end if;

       statement_5;

       <<Continue_2>> null;
    end loop;

Luckily, this is illegal. (GNAT generates the error message �target of
goto statement is not reachable�.)

> Final note: for Ada programmers who are incurably goto-challenged, you can
> achieve exactly the same effect as the above loop by using:
>
>    while Condition_1 loop
>    declare
>       Continue : exception;
>    begin
>       statement_1;
>
>       if Condition_2 then
>          raise Continue;
>       end if;
>
>       statement_3;
>
>    exception
>       when Continue => null;

     end; -- need one of these here

>    end loop;
>
> A kind compiler will translate this use of exceptions into a simple goto
> and bypass the normal exception mechanism. Why anyone would go to such
> circumlocutions escapes me, but some programmers will do almost anything
> to avoid the goto keyword, and besides there are silly coding standards
> around which totally forbid the use of the goto keyword.

This scheme avoids the name clash I described above, but there are
coding standards that would consider declaring, raising, and handling an
exception all within the same scope to be just as "naughty" as using a
goto. The argument is that an exception should represent part of the
interface of an abstraction, not part of its internal logic. Basically,
an exception should represent an error situation which the given
abstraction is able to _detect_, but which, by deliberate design, it is
not able to _handle_, because the policy for handling this situation is
beyond its chosen level of abstraction.  Raising and propagating the
exception out of the abstraction leaves it up to its clients to decide
the proper policy.  Otherwise, if the condition is just a "normal"
situation anticipated by the internal logic of the abstraction, then
"normal" control-flow constructs (it is felt) should be good enough.

I agree with Robert that resorting to a goto under these circumstances
is more reasonable and "honest" than "faking" a goto with an exception.
But I would tend to avoid the goto as well, and just live with the extra
level of nesting:

    while Condition_1 loop
       statement_1;

       if not Condition_2 then
          statement_3;
       end if;

    end loop;

    while Condition_3 loop
       statement_4;

       if not Condition_4 then
          statement_5;
       end if;

    end loop;

--
Internet.Usenet.Put_Signature
  (Name       => "John G. Volan",
   Employer   => "Raytheon/TI Advanced C3I Systems, San Jose, CA",
   Work_Email => "jvolan@ti.com",
   Home_Email => "johnvolan@sprintmail.com",
   Slogan     => "Ada95: World's *FIRST* International-Standard OOPL",
   Disclaimer => "My employer never defined these opinions, so using " &
                 "them would be totally erroneous...or is that just "  &
                 "nondeterministic behavior now? :-) ");

-------------------==== Posted via Deja News ====-----------------------
      http://www.dejanews.com/     Search, Read, Post to Usenet




      parent reply	other threads:[~1997-09-29  0:00 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-09-17  0:00 Is there an ADA analogue to the C++ continue statement? Heath, Terry D.
1997-09-18  0:00 ` Robert Dewar
1997-09-18  0:00 ` Pascal Obry
1997-09-18  0:00   ` Samuel Tardieu
1997-09-18  0:00   ` Robert A Duff
1997-09-19  0:00   ` Robert Dewar
     [not found]     ` <3422F037.41CA@lmco.com>
1997-09-20  0:00       ` dan13
1997-09-21  0:00         ` Robert Dewar
     [not found]           ` <3426B51E.7296@lmco.com>
1997-09-22  0:00             ` Coding Standards & GOTO Matthew Heaney
1997-09-23  0:00               ` Mark A Biggar
1997-09-24  0:00                 ` Shmuel (Seymour J.) Metz
1997-09-24  0:00                 ` W. Wesley Groleau x4923
1997-09-24  0:00               ` Aaron Quantz
1997-09-26  0:00               ` Charles H. Sampson
1997-09-23  0:00             ` Charles Rose
1997-09-24  0:00               ` Matthew Heaney
1997-09-25  0:00                 ` Shmuel (Seymour J.) Metz
1997-09-23  0:00             ` Coding Standards W. Wesley Groleau x4923
1997-09-22  0:00         ` Is there an ADA analogue to the C++ continue statement? Richard D Riehle
1997-09-23  0:00         ` GOTO considered Satanic (was: Is there an ADA analogue to the C++ continue statement?) Adam Beneschan
1997-09-24  0:00           ` W. Wesley Groleau x4923
1997-09-24  0:00           ` Brian Rogoff
1997-09-25  0:00             ` Larry Kilgallen
1997-09-26  0:00             ` Matthew Heaney
1997-09-26  0:00               ` Brian Rogoff
1997-10-07  0:00               ` Robert I. Eachus
1997-09-25  0:00           ` Alan Brain
1997-09-25  0:00             ` Shmuel (Seymour J.) Metz
1997-09-22  0:00     ` Is there an ADA analogue to the C++ continue statement? Richard A. O'Keefe
1997-09-29  0:00     ` John G. Volan [this message]
replies disabled

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