comp.lang.ada
 help / color / mirror / Atom feed
From: dewar@merv.cs.nyu.edu (Robert Dewar)
Subject: Re: Is there an ADA analogue to the C++ continue statement?
Date: 1997/09/19
Date: 1997-09-19T00:00:00+00:00	[thread overview]
Message-ID: <dewar.874665174@merv> (raw)
In-Reply-To: 5vqm61$fu2$1@cf01.edf.fr



Heath, Terry D. ask about an Ada equivalent for :

>         while (condition-1)
>               {
>                     statement-1;
>
>                     if (condition-2)
>                         continue;
>
>                     statement-3;
>                }


The most reasonable translation 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;

Now for some odd reason, many programmers have picked up allergies to gotos
that are spelled using the goto keyword, though these same programmers do
not mind gotos spelled with other keywords such as continue (the continue,
since it can rip you out of deeply nested structures, is of course a form
of goto, it just doesn't look like a goto, but it quacks like one :-)

Yes, yes, we all understand that general inappropriate use of gotos is
a BAD THING. For me that is just a special rule that general inappropriate
use of *any* feature in a language is a bad thing. I don't stop myself using
case statements in my programs just because some idiots misuse them, and
equally I have no problem using goto in a stylized way as shown above just
becausee some idiots misuse them.

Whether the continue is sufficiently important to include as a fundamental
gizmo is a continuing (ha ha) discussion in language design. Algol-60 did
not have this construct, and the algol family generally followed this
lead. Pascal for instance has no goto, and the one place you will find
goto used in Wirth's book on algorithms is precisely for the continue
construct given above (actually that's not quite true, there is another
place where he uses it for a break out on an exceptional condition, but
one would be more likely to use an exception for this purpose in Ada.

Yes, it is possible to use a Boolean flag instead, but the fiddling around
to do this can be a bit tortured if we contrive a deeply nested case.

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
   begin
  //// ooops let's try that again

   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 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.

Why silly? Well to me, most absolute rules are a mistake, since there are
almost always exceptions. Note that I avoid the mistake of phrasing this
particular rule in absolute terms :-)

Robert





  parent reply	other threads:[~1997-09-19  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   ` Robert A Duff
1997-09-18  0:00   ` Samuel Tardieu
1997-09-19  0:00   ` Robert Dewar [this message]
     [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             ` Coding Standards W. Wesley Groleau x4923
1997-09-23  0:00             ` Coding Standards & GOTO Charles Rose
1997-09-24  0:00               ` Matthew Heaney
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 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
replies disabled

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