[fpc-pascal] Illegal counter variable?

James Richters james at productionautomation.net
Mon Sep 9 14:10:19 CEST 2019


I don't understand what difference it makes if I assign I :=0 with the variable declaration or If I was using I for something else..  It's irrelevant what the condition of I was before the for loop, just as it's irrelevant that the status of a variable is before you assign it to something.    I just don't think this should produce any kind of an error,  I'm not doing anything technically wrong as far as I can tell.
I should be able to use I for a whole pile of things then re-cycle it in my for loop.. then use it for more stuff after.. the condition of I before or after the for loop is irrelevant, as long as I make sure I initialize it before using it.. everything should be fine.   I should be able to do this if I want:

Var
   I : Longint = 57;
Begin
Inc(I);
Writeln(I);
I:=50;
Inc(I);
Writeln(I);
For I := 1 to 6 Do
  Writeln(I);
I:=-10;
Inc(I);
Writeln(I);
End.

It should not matter what I was using I for before or after the for loop or if it was initialized or used for some other purpose before or after the loop.

James
-----Original Message-----
From: fpc-pascal <fpc-pascal-bounces at lists.freepascal.org> On Behalf Of Mattias Gaertner via fpc-pascal
Sent: Monday, September 9, 2019 5:59 AM
To: fpc-pascal at lists.freepascal.org
Cc: Mattias Gaertner <nc-gaertnma at netcologne.de>
Subject: Re: [fpc-pascal] Illegal counter variable?

On Mon, 9 Sep 2019 11:44:05 +0200
SPRL AFOR <aforsprl at gmail.com> wrote:

> Hi.
> 
> Index variable can neither be initialized nor computed. Read 
> https://wiki.lazarus.freepascal.org/FOR..DO

That page only talks about assigning the loop var *inside* the loop, which is forbidden.

James question is about the difference between an initialized global var and a non initialized global var.

My guess is that it has to do with the different exe/linker sections where these vars are put. Hopefully some compiler guru can explain.

Mattias

_______________________________________________
fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


More information about the fpc-pascal mailing list