comp.lang.ada
 help / color / mirror / Atom feed
From: "DuckE" <nospam_steved@pacifier.com>
Subject: Re: checking a loop execution
Date: 2000/04/19
Date: 2000-04-19T00:00:00+00:00	[thread overview]
Message-ID: <38fe720b.0@news.pacifier.com> (raw)
In-Reply-To: 8dl76u$etf$1@nnrp1.deja.com

First: Your example will result in an undefined value for ivar if
<condition> is never met.

Second: I sometimes use the construct:

  procedure loop_test is
  begin
    for i in 1 .. 10 loop
      if <condition> then
        put( i, 2 );
        return;
      end if;
    end loop;
    put( defaultValue, 2 ); -- default value when condition is never met
  end loop_test;

  I don't know if it's more or less efficient, but it does eliminate the
extra variable.

  BTW: I usually avoid having more than one exit point from a procedure, but
in
  a routine this small I think it actually reads better.

  SteveD

<pumilia@my-deja.com> wrote in message news:8dl76u$etf$1@nnrp1.deja.com...
> Is there a way to check if a loop has been exited before completion or
> if all iterations have been performed?
> In fortran the loop index is increased one last time, at the end of
> the loop, but in Ada that trick cannot be exploited; the index is not
> even defined outside the loop.
> I could add a control variable (ivar, in my example) to take care of the
> iteration number when exiting, but that would decrease the peformance
> of my algorithm
>
> Example
>
> procedure loop_test  is
>
> ivar : integer;
>
> begin
>  for i in 1 .. 10 loop
>  if <condition>  then
>   ivar := i;
>   exit ;
>  end if ;
>  end loop;
>  put(ivar,2);
> end loop_test;
>
>
>
> thank you for your suggestions
>
> Pol
>
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.






  reply	other threads:[~2000-04-19  0:00 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-04-19  0:00 checking a loop execution pumilia
2000-04-19  0:00 ` DuckE [this message]
2000-04-20  0:00 ` Geoff Bull
2000-04-20  0:00 ` Andreas Schulz
2000-04-20  0:00 ` tmoran
2000-04-20  0:00   ` Charles Hixson
2000-04-20  0:00 ` chad
2000-04-21  0:00   ` Robert Dewar
2000-04-21  0:00   ` Robert Dewar
2000-04-22  0:00     ` Chad R. Meiners
2000-04-23  0:00       ` Robert Dewar
2000-04-24  0:00         ` Ehud Lamm
2000-04-24  0:00           ` Ehud Lamm
2000-04-24  0:00       ` Bill Greene
replies disabled

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