comp.lang.ada
 help / color / mirror / Atom feed
From: "Robert I. Eachus" <rieachus@attbi.com>
Subject: Re: Trivial Ada question
Date: Wed, 03 Jul 2002 18:59:27 GMT
Date: 2002-07-03T18:59:27+00:00	[thread overview]
Message-ID: <3D234A17.2070508@attbi.com> (raw)
In-Reply-To: mailman.1025258942.18931.comp.lang.ada@ada.eu.org

"Reinert Korsnes" <reinert.korsnes@chello.no> asked:

>I would like to ask about a program construct:
>
>I want to test if some condition holds for all values
>of I in A'range and in this case the variable
>N should be incremented by one ( N := N + 1; ).
>How can I do this most elegant and effective with Ada95
>(without "goto") ?
>Assume I do not want to make a separate subroutine for this.
>
>This example is simple but not computationally optimal (two tests):
>
>    for I in A'range loop
>        exit when not some condition(I);
>        if I = A'last then
>-- Everything OK to the end:
>           N := N + 1;
>        end if;
>    end loop;


This is fine.  I don't know if your compiler will figure out how to 
merge the loop end and the assignment to N, but the necessary 
information is certainly there.

However, that points out the reason you shouldn't even be asking the 
question.  The code:

    for I in A'range loop
     if some_condition(I) then goto Skip; end if;
    end loop;
    N := N + 1;
<<Skip>> ...

Is certainly short and sweet, and will compile to exactly what you want. 
  Ah, but it uses that evil incarnate, a goto!  Let's think about this 
for a minute.  Why should you avoid gotos?  Well because they can result 
in horrible things like loops with multiple exits.  But that is 
precisely what we are trying to write here, a loop with two exits.

And this is the problem with draconian no goto policies.  The eliminate 
a symptom but don't address the underlying construct.  That is 
fortunate, because sometimes you need those constructs.  Notice that a 
compiler might generate identical code for these two examples.  What 
makes one right and the other wrong?



 




  parent reply	other threads:[~2002-07-03 18:59 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-06-28  9:07 Trivial Ada question Reinert Korsnes
2002-06-28 10:10 ` David C. Hoos, Sr.
2002-06-28 10:54   ` Reinert Korsnes
2002-06-28 10:57     ` Reinert Korsnes
2002-06-28 11:33       ` David C. Hoos, Sr.
2002-06-28 11:44     ` Ted Dennison
2002-06-28 21:38       ` Steven Deller
2002-06-28 12:29     ` Steve Sangwine
2002-07-03 18:59   ` Robert I. Eachus [this message]
2002-07-03 19:41     ` Darren New
2002-07-04  8:28       ` Lutz Donnerhacke
  -- strict thread matches above, loose matches on Subject: below --
2002-07-04  8:59 Grein, Christoph
2002-07-04  9:15 ` Lutz Donnerhacke
2002-07-05 22:25 ` Robert I. Eachus
replies disabled

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