Recent

Author Topic: [SOLVED] What is 'n' about in this code?  (Read 1123 times)

Slyde

  • Full Member
  • ***
  • Posts: 152
[SOLVED] What is 'n' about in this code?
« on: November 23, 2020, 05:52:29 am »
Through a search here on the Forum, I found an explanation 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;
  22.   ReadLn;
  23. end.

What's the purpose of this uninitialized n?
« Last Edit: November 23, 2020, 09:56:14 pm by Slyde »
Linux Mint 21.3
Lazarus 3.0

abunakov

  • Newbie
  • Posts: 3
Re: What is 'n' about in this code?
« Reply #1 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.

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: What is 'n' about in this code?
« Reply #2 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, 0
8 - 7, 0
7 - 6, 0
6 - 5, 0
5 - 4, 0
« Last Edit: November 23, 2020, 09:10:59 am by Thaddy »
Specialize a type, not a var.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: What is 'n' about in this code?
« Reply #3 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.

Despite what many seem to think the base manuals are quite good ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Slyde

  • Full Member
  • ***
  • Posts: 152
Re: What is 'n' about in this code?
« Reply #4 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?
« Last Edit: November 23, 2020, 11:52:51 pm by Slyde »
Linux Mint 21.3
Lazarus 3.0

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: What is 'n' about in this code?
« Reply #5 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.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018