comp.lang.ada
 help / color / mirror / Atom feed
* Trivial Ada question
@ 2002-06-28  9:07 Reinert Korsnes
  2002-06-28 10:10 ` David C. Hoos, Sr.
  0 siblings, 1 reply; 14+ messages in thread
From: Reinert Korsnes @ 2002-06-28  9:07 UTC (permalink / raw)


Hi,

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 example is ugly but more computationally optimal (one test):

    for I in A'range loop
        if "not some condition(i)" then
           N := N - 1;
           exit;
        end if;
    end loop;
    N := N + 1;

Tricks by "named loops" also seem ugly.

reinert




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

* Re: Trivial Ada question
  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-07-03 18:59   ` Robert I. Eachus
  0 siblings, 2 replies; 14+ messages in thread
From: David C. Hoos, Sr. @ 2002-06-28 10:10 UTC (permalink / raw)


How about

All_OK : Boolean;
.
.
.
All_OK := True;
for I in A'Range loop
   if not some condition (I) then
      All_OK := False;
      exit;
   end if;
end loop;
if ALL_OK then
  N := N + 1;
end if;

----- Original Message ----- 
From: "Reinert Korsnes" <reinert.korsnes@chello.no>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: June 28, 2002 4:07 AM
Subject: Trivial Ada question


> Hi,
> 
> 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 example is ugly but more computationally optimal (one test):
> 
>     for I in A'range loop
>         if "not some condition(i)" then
>            N := N - 1;
>            exit;
>         end if;
>     end loop;
>     N := N + 1;
> 
> Tricks by "named loops" also seem ugly.
> 
> reinert
> 
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
> 
> 





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

* Re: Trivial Ada question
  2002-06-28 10:10 ` David C. Hoos, Sr.
@ 2002-06-28 10:54   ` Reinert Korsnes
  2002-06-28 10:57     ` Reinert Korsnes
                       ` (2 more replies)
  2002-07-03 18:59   ` Robert I. Eachus
  1 sibling, 3 replies; 14+ messages in thread
From: Reinert Korsnes @ 2002-06-28 10:54 UTC (permalink / raw)


Yes, maybe the simplest possible.
The example below is also somehow simple,
but I hoped for some "exit name" solution
which I have not yet noticed :-)

reinert

 All_OK := 1;
 for I in A'Range loop
    if not some condition (I) then
       All_OK := 1;
       exit;
    end if;
 end loop;
 N := N + All_OK;


David C. Hoos, Sr. wrote:

> How about
> 
> All_OK : Boolean;
> .
> .
> .
> All_OK := True;
> for I in A'Range loop
>    if not some condition (I) then
>       All_OK := False;
>       exit;
>    end if;
> end loop;
> if ALL_OK then
>   N := N + 1;
> end if;
> 
> ----- Original Message -----
> From: "Reinert Korsnes" <reinert.korsnes@chello.no>
> Newsgroups: comp.lang.ada
> To: <comp.lang.ada@ada.eu.org>
> Sent: June 28, 2002 4:07 AM
> Subject: Trivial Ada question
> 
> 
>> Hi,
>> 
>> 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 example is ugly but more computationally optimal (one test):
>> 
>>     for I in A'range loop
>>         if "not some condition(i)" then
>>            N := N - 1;
>>            exit;
>>         end if;
>>     end loop;
>>     N := N + 1;
>> 
>> Tricks by "named loops" also seem ugly.
>> 
>> reinert
>> 
>> _______________________________________________
>> comp.lang.ada mailing list
>> comp.lang.ada@ada.eu.org
>> http://ada.eu.org/mailman/listinfo/comp.lang.ada
>> 
>>




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

* Re: Trivial Ada question
  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 12:29     ` Steve Sangwine
  2 siblings, 1 reply; 14+ messages in thread
From: Reinert Korsnes @ 2002-06-28 10:57 UTC (permalink / raw)


Correction below.  It always happens :-)

reinert

Reinert Korsnes wrote:

> Yes, maybe the simplest possible.
> The example below is also somehow simple,
> but I hoped for some "exit name" solution
> which I have not yet noticed :-)
> 
> reinert
> 
>  All_OK := 1;
>  for I in A'Range loop
>     if not some condition (I) then
>        All_OK := 1;

obs, I mean All_OK := 0;

>        exit;
>     end if;
>  end loop;
>  N := N + All_OK;
> 
> 
> David C. Hoos, Sr. wrote:
> 
>> How about
>> 
>> All_OK : Boolean;
>> .
>> .
>> .
>> All_OK := True;
>> for I in A'Range loop
>>    if not some condition (I) then
>>       All_OK := False;
>>       exit;
>>    end if;
>> end loop;
>> if ALL_OK then
>>   N := N + 1;
>> end if;
>> 
>> ----- Original Message -----
>> From: "Reinert Korsnes" <reinert.korsnes@chello.no>
>> Newsgroups: comp.lang.ada
>> To: <comp.lang.ada@ada.eu.org>
>> Sent: June 28, 2002 4:07 AM
>> Subject: Trivial Ada question
>> 
>> 
>>> Hi,
>>> 
>>> 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 example is ugly but more computationally optimal (one test):
>>> 
>>>     for I in A'range loop
>>>         if "not some condition(i)" then
>>>            N := N - 1;
>>>            exit;
>>>         end if;
>>>     end loop;
>>>     N := N + 1;
>>> 
>>> Tricks by "named loops" also seem ugly.
>>> 
>>> reinert
>>> 
>>> _______________________________________________
>>> comp.lang.ada mailing list
>>> comp.lang.ada@ada.eu.org
>>> http://ada.eu.org/mailman/listinfo/comp.lang.ada
>>> 
>>>




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

* Re: Trivial Ada question
  2002-06-28 10:57     ` Reinert Korsnes
@ 2002-06-28 11:33       ` David C. Hoos, Sr.
  0 siblings, 0 replies; 14+ messages in thread
From: David C. Hoos, Sr. @ 2002-06-28 11:33 UTC (permalink / raw)


If N is not referenced within the loop you could do:

for I in A'Range loop
   if not some condition (I) then
      N := N - 1;
      exit;
   end if;
end loop;
N := N + 1;

----- Original Message ----- 
From: "Reinert Korsnes" <reinert.korsnes@chello.no>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: June 28, 2002 5:57 AM
Subject: Re: Trivial Ada question


> Correction below.  It always happens :-)
> 
> reinert
> 
> Reinert Korsnes wrote:
> 
> > Yes, maybe the simplest possible.
> > The example below is also somehow simple,
> > but I hoped for some "exit name" solution
> > which I have not yet noticed :-)
> > 
> > reinert
> > 
> >  All_OK := 1;
> >  for I in A'Range loop
> >     if not some condition (I) then
> >        All_OK := 1;
> 
> obs, I mean All_OK := 0;
> 
> >        exit;
> >     end if;
> >  end loop;
> >  N := N + All_OK;
> > 
> > 
> > David C. Hoos, Sr. wrote:
> > 
> >> How about
> >> 
> >> All_OK : Boolean;
> >> .
> >> .
> >> .
> >> All_OK := True;
> >> for I in A'Range loop
> >>    if not some condition (I) then
> >>       All_OK := False;
> >>       exit;
> >>    end if;
> >> end loop;
> >> if ALL_OK then
> >>   N := N + 1;
> >> end if;
> >> 
> >> ----- Original Message -----
> >> From: "Reinert Korsnes" <reinert.korsnes@chello.no>
> >> Newsgroups: comp.lang.ada
> >> To: <comp.lang.ada@ada.eu.org>
> >> Sent: June 28, 2002 4:07 AM
> >> Subject: Trivial Ada question
> >> 
> >> 
> >>> Hi,
> >>> 
> >>> 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 example is ugly but more computationally optimal (one test):
> >>> 
> >>>     for I in A'range loop
> >>>         if "not some condition(i)" then
> >>>            N := N - 1;
> >>>            exit;
> >>>         end if;
> >>>     end loop;
> >>>     N := N + 1;
> >>> 
> >>> Tricks by "named loops" also seem ugly.
> >>> 
> >>> reinert
> >>> 
> >>> _______________________________________________
> >>> comp.lang.ada mailing list
> >>> comp.lang.ada@ada.eu.org
> >>> http://ada.eu.org/mailman/listinfo/comp.lang.ada
> >>> 
> >>>
> 
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
> 
> 





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

* Re: Trivial Ada question
  2002-06-28 10:54   ` Reinert Korsnes
  2002-06-28 10:57     ` Reinert Korsnes
@ 2002-06-28 11:44     ` Ted Dennison
  2002-06-28 21:38       ` Steven Deller
  2002-06-28 12:29     ` Steve Sangwine
  2 siblings, 1 reply; 14+ messages in thread
From: Ted Dennison @ 2002-06-28 11:44 UTC (permalink / raw)


Reinert Korsnes wrote:
> Yes, maybe the simplest possible.
> The example below is also somehow simple,
> but I hoped for some "exit name" solution
> which I have not yet noticed :-)
> 
> reinert
> 
>  All_OK := 1;
(0 changed to 1)

>  for I in A'Range loop
>     if not some condition (I) then
>        All_OK := 0;
(1 changed to 0)

>        exit;
>     end if;
>  end loop;
>  N := N + All_OK;


Hmmmm. How about:

function All_Some_Condition (A : Some_Array; N : Natural) return Natural is
begin
    for I in A'range loop
       if not some condition A(I) then
          return N;
       end loop;
    end loop;

    return N + 1;
end All_Some_Condition;

(Warning: Not compiled. A'range may need to be A'first..A'last, I always 
get those mixed up)




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

* Re: Trivial Ada question
  2002-06-28 10:54   ` Reinert Korsnes
  2002-06-28 10:57     ` Reinert Korsnes
  2002-06-28 11:44     ` Ted Dennison
@ 2002-06-28 12:29     ` Steve Sangwine
  2 siblings, 0 replies; 14+ messages in thread
From: Steve Sangwine @ 2002-06-28 12:29 UTC (permalink / raw)


On Fri, 28 Jun 2002 11:54:50 +0100, Reinert Korsnes
<reinert.korsnes@chello.no> wrote:

How about:

declare
  All_OK : Boolean := True;
begin 
  for I in A'Range loop
    All_OK := All_OK and Some_Condition(I);
    exit when not All_OK;
  end loop;
  if All_OK then
    N := N + 1;
  end if;
end;

This has the benefit of being clear (almost self explanatory), but
maybe not as concise as would be liked.

Steve Sangwine

>Yes, maybe the simplest possible.
>The example below is also somehow simple,
>but I hoped for some "exit name" solution
>which I have not yet noticed :-)
>
>reinert
>
> All_OK := 1;
> for I in A'Range loop
>    if not some condition (I) then
>       All_OK := 1;
>       exit;
>    end if;
> end loop;
> N := N + All_OK;
>
>
>David C. Hoos, Sr. wrote:
>
>> How about
>> 
>> All_OK : Boolean;
>> .
>> .
>> .
>> All_OK := True;
>> for I in A'Range loop
>>    if not some condition (I) then
>>       All_OK := False;
>>       exit;
>>    end if;
>> end loop;
>> if ALL_OK then
>>   N := N + 1;
>> end if;
>> 
>> ----- Original Message -----
>> From: "Reinert Korsnes" <reinert.korsnes@chello.no>
>> Newsgroups: comp.lang.ada
>> To: <comp.lang.ada@ada.eu.org>
>> Sent: June 28, 2002 4:07 AM
>> Subject: Trivial Ada question
>> 
>> 
>>> Hi,
>>> 
>>> 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 example is ugly but more computationally optimal (one test):
>>> 
>>>     for I in A'range loop
>>>         if "not some condition(i)" then
>>>            N := N - 1;
>>>            exit;
>>>         end if;
>>>     end loop;
>>>     N := N + 1;
>>> 
>>> Tricks by "named loops" also seem ugly.
>>> 
>>> reinert
>>> 
>>> _______________________________________________
>>> comp.lang.ada mailing list
>>> comp.lang.ada@ada.eu.org
>>> http://ada.eu.org/mailman/listinfo/comp.lang.ada
>>> 
>>>




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

* RE: Trivial Ada question
  2002-06-28 11:44     ` Ted Dennison
@ 2002-06-28 21:38       ` Steven Deller
  0 siblings, 0 replies; 14+ messages in thread
From: Steven Deller @ 2002-06-28 21:38 UTC (permalink / raw)


I'd prefer
   ...
   function All_Some_Condition (A : Some_Array) return Boolean is
   begin
    for I in A'range loop
       if not some_condition A(I) then
          return False ;
       end if ;
    end loop ;
    return True ;
   end All_Some_Condition ;
 begin
   ...
   if All_Some_Condition(A) then
    N := N+1 ;
   end if ;
   ...

That seems to make the intention clearer and the main-line code is easy
to read as well.

If you have a lot of "conditions" then you could make a generic
"All_Are" and instantiate with a function computing the necessary
condition.  In the case above, you are simply writing an explicit
instance, e.g.
    function All_True ...
      ...
         if not A(I) then ...

or
    function All_Less_Than_100 ...
      ...
         if not ( A() < 100 ) ...

Regards,
Steve




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

* Re: Trivial Ada question
  2002-06-28 10:10 ` David C. Hoos, Sr.
  2002-06-28 10:54   ` Reinert Korsnes
@ 2002-07-03 18:59   ` Robert I. Eachus
  2002-07-03 19:41     ` Darren New
  1 sibling, 1 reply; 14+ messages in thread
From: Robert I. Eachus @ 2002-07-03 18:59 UTC (permalink / raw)


"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?



 




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

* Re: Trivial Ada question
  2002-07-03 18:59   ` Robert I. Eachus
@ 2002-07-03 19:41     ` Darren New
  2002-07-04  8:28       ` Lutz Donnerhacke
  0 siblings, 1 reply; 14+ messages in thread
From: Darren New @ 2002-07-03 19:41 UTC (permalink / raw)


"Robert I. Eachus" wrote:
> Notice that a
> compiler might generate identical code for these two examples.  What
> makes one right and the other wrong?

Because the compiler isn't the only one that reads the code?

"exit when ..." says to leave the innermost loop.
"goto skip" doesn't tell you anything about what "skip" means.

-- 
Darren New 
San Diego, CA, USA (PST). Cryptokeys on demand.
** http://home.san.rr.com/dnew/DNResume.html **
** http://images.fbrtech.com/dnew/ **

 Proud to live in a country where "fashionable" 
     is denim, "stone-washed" faded, with rips.



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

* Re: Trivial Ada question
  2002-07-03 19:41     ` Darren New
@ 2002-07-04  8:28       ` Lutz Donnerhacke
  0 siblings, 0 replies; 14+ messages in thread
From: Lutz Donnerhacke @ 2002-07-04  8:28 UTC (permalink / raw)


* Darren New wrote:
>"Robert I. Eachus" wrote:
>> Notice that a
>> compiler might generate identical code for these two examples.  What
>> makes one right and the other wrong?
>
>Because the compiler isn't the only one that reads the code?
>
>"exit when ..." says to leave the innermost loop.
>"goto skip" doesn't tell you anything about what "skip" means.

So simply write:

  INCREMENT: for dummy in 1 .. 1 loop
    for i in a'Range loop
      exit INCREMENT when condition (a (i));
    end loop;
    n := n + 1;
  end loop;

No 'evil' goto, and a 'telling' label.



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

* Re: Trivial Ada question
@ 2002-07-04  8:59 Grein, Christoph
  2002-07-04  9:15 ` Lutz Donnerhacke
  2002-07-05 22:25 ` Robert I. Eachus
  0 siblings, 2 replies; 14+ messages in thread
From: Grein, Christoph @ 2002-07-04  8:59 UTC (permalink / raw)


> * Darren New wrote:
> >"Robert I. Eachus" wrote:

[I do not remeber the exact form of Eachus', it was something like the 
following:]

  for i in A'Range loop
    if not condition (A (i)) then
      goto Dont_Increment;
    end if;
  end loop;
  n := n + 1;
  <<Dont_Increment>>

> >> Notice that a
> >> compiler might generate identical code for these two examples.  What
> >> makes one right and the other wrong?
> >
> >Because the compiler isn't the only one that reads the code?
> >
> >"exit when ..." says to leave the innermost loop.
> >"goto skip" doesn't tell you anything about what "skip" means.
> 
> So simply write:
> 
>   INCREMENT: for dummy in 1 .. 1 loop
>     for i in a'Range loop
>       exit INCREMENT when condition (a (i));
>     end loop;
>     n := n + 1;
>   end loop;
> 
> No 'evil' goto, and a 'telling' label.

This was a joke, wasn't it? Is this really better than a simle and innocent and 
nearby goto? A one-time 'loop' enclosing another work loop...

These goto wars are astounding. Event the most simple goto solution is replaced 
by a contrived no-goto one!

I have to confess that I have not needed a goto in so many years, but the goto 
solution is one of the simplest solutions of all 
those presented.



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

* Re: Trivial Ada question
  2002-07-04  8:59 Grein, Christoph
@ 2002-07-04  9:15 ` Lutz Donnerhacke
  2002-07-05 22:25 ` Robert I. Eachus
  1 sibling, 0 replies; 14+ messages in thread
From: Lutz Donnerhacke @ 2002-07-04  9:15 UTC (permalink / raw)


* Grein, Christoph wrote:
>> So simply write:
>> 
>>   INCREMENT: for dummy in 1 .. 1 loop
>>     for i in a'Range loop
>>       exit INCREMENT when condition (a (i));
>>     end loop;
>>     n := n + 1;
>>   end loop;
>> 
>> No 'evil' goto, and a 'telling' label.
>
>This was a joke, wasn't it?

No. It is a solution for people living in eviroments where writing 'goto'
into programms may cause losing jobs.

>Is this really better than a simle and innocent and nearby goto?

No.

>These goto wars are astounding.

Ack.

>Event the most simple goto solution is replaced by a contrived no-goto
>one!

Ack.

>I have to confess that I have not needed a goto in so many years, but the
>goto solution is one of the simplest solutions of all those presented.

Ack.



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

* Re: Trivial Ada question
  2002-07-04  8:59 Grein, Christoph
  2002-07-04  9:15 ` Lutz Donnerhacke
@ 2002-07-05 22:25 ` Robert I. Eachus
  1 sibling, 0 replies; 14+ messages in thread
From: Robert I. Eachus @ 2002-07-05 22:25 UTC (permalink / raw)


Grein, Christoph wrote:


> This was a joke, wasn't it? Is this really better than a simle and innocent and 
> nearby goto? A one-time 'loop' enclosing another work loop...
...
> I have to confess that I have not needed a goto in so many years, but the goto 
> solution is one of the simplest solutions of all those presented.


I totally agree.  However, there is a "reasonable" nested loop version...

   INCREMENT:
     loop
       for I in A'Range loop
         exit INCREMENT when condition (A (I));
       end loop;
       N := N + 1;
       exit INCREMENT;
     end loop;

Again the compiler has a chance to generate the correct simple code. 
But we still haven't done anything about the fact that the inner loop 
has two exits that need to be tested.  (The outer loop has a single exit 
and doesn't add any real complexity to the program flow.)

Which is why this discussion sort of bothers me.  The underlying 
complexity of the algorithm can be hidden, but who are you hiding it 
from?  It had better not be the test team.  And that is why I like the 
goto here.  It screams to the testers (and other readers) that the water 
  is deep here.  You have to insert a mental bookmark and read the two 
paths separately.

In fact, the I would probably stick in a comment:
  -- This loop must have two exits.

It is the "must have" that is key, not how it is implemented.




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

end of thread, other threads:[~2002-07-05 22:25 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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