# Lazarus

## Programming => General => Topic started by: Slyde on November 23, 2020, 05:52:29 am

Title: [SOLVED] What is 'n' about in this code?
Post by: Slyde on November 23, 2020, 05:52:29 am
Through a search here on the Forum, I found an explanation (https://forum.lazarus.freepascal.org/index.php/topic,45003.msg317376.html#msg317376) of FP vectors.  My interest is in Thaddy's code, third from the bottom.

Code: Pascal  [Select][+][-]
1. Program HelloSTL;
2. {\$mode delphi}  // mode doesn't matter
3. uses gvector;
4. type
5.   iVector = TVector<LongInt>;
6.
7. var
8. v : iVector;
9. n, i : longint;
10.
11. begin
12.   v := iVector.Create;
13.   // Push 10 elements
14.   for i := 0 to 9 do
15.     v.PushBack(i);
16.   // Pop all elements ?
17.   for n in v do    <------WHAT DOES n DO?
18.   begin
19.     v.PopBack(); // this lowers the number of elements by one
20.     WriteLn(v.Size(), ' - ', v.Back(), ', ', v.Front()); // so this should be eventually max 5, not 10,certainly not 20.
21.   end;
23. end.

What's the purpose of this uninitialized n?
Title: Re: What is 'n' about in this code?
Post by: abunakov on November 23, 2020, 08:01:47 am
for n in v do is a language construct which is called an iterator. It basically means "loop over a collection v and put the value of each element into n".

So in this case n is initialized at the beginning fo each iteration with the value of the current element. But n is not being used in the iterator body for the purposes of this particular example. So the value of n is assigned but is just being ignored.
Title: Re: What is 'n' about in this code?
Post by: Thaddy on November 23, 2020, 09:02:59 am
2 remarks:
1. n is not necessary. You can re-use i, since it is longint too. Its meaning changes to element instead of index. n is merely for clarity.
2. On my debian Linux the value is indeed 5 elements, 4 as Back and 0 as front, as you expected.
Code: [Select]
`9 - 8, 08 - 7, 07 - 6, 06 - 5, 05 - 4, 0`
Title: Re: What is 'n' about in this code?
Post by: lucamar on November 23, 2020, 12:23:37 pm
Yo can spare yourself a lot of grief and bewilderig by reading the manuals. In this case, the Language Guide, section: The For..in..do statement (https://freepascal.org/docs-html/current/ref/refsu58.html).

Despite what many seem to think the base manuals are quite good ;)
Title: Re: What is 'n' about in this code?
Post by: Slyde on November 23, 2020, 07:46:01 pm
Okay.  I got that stuff down.  I was just thinking that n was some kind of special voodoo thing because it stops the popback() dead in its tracks at half the vector size it started with.  For my clarity, and at the risk of looking stupider than I already do, why does the popback() for-loop terminate at size = 5 and not completely zero out?

@lucamar - Thanks a million for the manual link.

OHHHH!  It's because n=5 and the vector now equals 5, so it's done. That it's at zero-out state! Right?
Title: Re: What is 'n' about in this code?
Post by: lucamar on November 23, 2020, 09:41:29 pm
OHHHH!  It's because n=5 and the vector now equals 5, so it's done. That's its zero-out state! Right?

Yes, that's in fact what the example tries to demonstrate, as you can see in the comments in the next lines. After five iterations the vector is empty, so there is no "next n" and the loop ends.