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

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