comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew J Heaney <mheaney@on2.com>
Subject: Re: How to make like Fortran "do i = 1,20,2"
Date: 2000/07/27
Date: 2000-07-27T00:00:00+00:00	[thread overview]
Message-ID: <7rhf9bb362.fsf@butter.albany.duck.com> (raw)
In-Reply-To: 39805669.3E7CF6CF@lmtas.lmco.com

Gary Scott <Gary.L.Scott@lmtas.lmco.com> writes:

> Hmmm, these and similar examples posted do not make Ada look very
> elegant...it makes a very simple concept seem somewhat convoluted.

This "very simple concept" is a major source of bugs -- that's why it's
not in the language.

If you want to increment by 2 through the loop, it's simple enough:

declare
   I : Positive := A'First;
begin
   while I <= A'Last loop
      ... A(I) ...
      I := I + 2;
   end loop;
end;

QED


If you want to do odd-ball things, then sometimes that requires extra
work.  The common things (like incrementing the loop index by 1) are
easy to do.

I can't even remember the last time I had to iterate over an array by an
increment other than 1.

Consider this analogy.  Suppose you want to iterate over a linked list:

   list<int> intList;
...
   list<int>::iterator iter = intList.begin();

In the normal case, when you increment by 1, it's simple:

   while (iter != intList.end())
   {
     ...
     iter++;
   }


Asking how to iterate in increments of 2 is like asking, How do I visit
every other list element?  That's an odd-ball thing to do, so you have
to do extra work.  (Try it and you'll see.)









  parent reply	other threads:[~2000-07-27  0:00 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-07-27  0:00 How to make like Fortran "do i = 1,20,2" Reinert Korsnes
2000-07-27  0:00 ` G. de Montmollin
2000-07-27  0:00 ` Lutz Donnerhacke
2000-07-27  0:00 ` Ken Garlington
2000-07-27  0:00 ` des walker
2000-07-27  0:00   ` tmoran
2000-07-28  0:00     ` Reinert Korsnes
2000-07-28  0:00       ` Gautier
2000-07-29  0:00       ` tmoran
2000-07-27  0:00   ` Gary Scott
2000-07-27  0:00 ` Larry Kilgallen
2000-07-27  0:00   ` Gary Scott
2000-07-27  0:00     ` Larry Kilgallen
2000-07-27  0:00     ` mjsilva
2000-07-27  0:00     ` Matthew J Heaney [this message]
2000-07-28  0:00       ` Gary Scott
2000-07-28  0:00         ` mjsilva
2000-07-29  0:00         ` Ehud Lamm
2000-07-28  0:00           ` Richard Riehle
     [not found]           ` <39833637.3B83BFAC@lmtas.lmco.com>
2000-07-29  0:00             ` Gary Scott
2000-07-29  0:00           ` Robert I. Eachus
2000-07-27  0:00     ` Pat Rogers
replies disabled

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