comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <stephen_leake@stephe-leake.org>
Subject: Re: "for E of Vector1 loop" equivalent  to "for n in Vector1.First_Index ..  Vector1.Last_Index loop ?
Date: Mon, 19 Sep 2016 00:37:09 -0700 (PDT)
Date: 2016-09-19T00:37:09-07:00	[thread overview]
Message-ID: <5113a69b-9b18-4a2f-999d-d32db69e3681@googlegroups.com> (raw)
In-Reply-To: <23ee42b9-8145-4d15-a745-a264505be8ff@googlegroups.com>

On Monday, September 19, 2016 at 2:23:10 AM UTC-5, reinkor wrote:
> Assume Vector1 is a vector (container) and I want to loop through it in the direction Vector1.First_Index .. Vector1.Last_Index.
> 
> Is the following equivalent:
> 
> for n in Vector1.First_Index .. Vector1-Last_Index loop
>    <do domething with Vector1(n)>
> end loop;
> 
> and 
> 
> for E of Vector1 loop
>   <do something with E>
> end loop;

Yes

> It is for me not directly intuitive that the latter construct
> process the vector elements in the same order as the former. 
> I feel I should find the answer in the definition of Ada - but din not :-)

See LRM 5.5.2 10:

For a generalized iterator, the loop parameter is created, the
iterator_name is evaluated, and the denoted iterator object becomes the
loop iterator.  In a forward generalized iterator, the operation First
of the iterator type is called on the loop iterator, to produce the
initial value for the loop parameter.  If the result of calling
Has_Element on the initial value is False, then the execution of the
loop_statement is complete.  Otherwise, the sequence_of_statements is
executed and then the Next operation of the iterator type is called with
the loop iterator and the current value of the loop parameter to produce
the next value to be assigned to the loop parameter.  This repeats until
the result of calling Has_Element on the loop parameter is False, or the
loop is left as a consequence of a transfer of control.  For a reverse
generalized iterator, the operations Last and Previous are called rather
than First and Next.


  reply	other threads:[~2016-09-19  7:37 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-19  7:23 "for E of Vector1 loop" equivalent to "for n in Vector1.First_Index .. Vector1.Last_Index loop ? reinkor
2016-09-19  7:37 ` Stephen Leake [this message]
2016-09-19  7:50   ` reinkor
2016-09-19  8:02     ` Egil H H
2016-09-19  8:41       ` reinkor
replies disabled

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