Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2019-09

Illegal counter variable?

31 messages · 13 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

James Richters

Can someone please tell me why this happens?

Var
   I : Longint = 0;
Begin
For I := 1 to 6 Do
   Writeln(I);
End.
This gives me :
initialize.pas(4,5) Error: Illegal counter variable
initialize.pas(6,4) Fatal: There were 1 errors compiling module, stopping
initialize.pas(0) Fatal: Compilation aborted
but this works fine:
Var
   I : Longint;
Begin
I := 0;
For I := 1 to 6 Do
   Writeln(I);
End.

Shouldn't these two programs be identical? What difference does it make if I initialize the variable in the Var section?

James

SPRL AFOR
to James Richters

Hi.

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

Antonio

Le 09/09/2019 à 11:30, James Richters a écrit :

Show quoted text 29 lines
Can someone please tell me why this happens? Var I : Longint = 0; Begin For I := 1 to 6 Do Writeln(I); End. This gives me : initialize.pas(4,5) Error: Illegal counter variable initialize.pas(6,4) Fatal: There were 1 errors compiling module, stopping initialize.pas(0) Fatal: Compilation aborted but this works fine: Var I : Longint; Begin I := 0; For I := 1 to 6 Do Writeln(I); End. Shouldn't these two programs be identical? What difference does it make if I initialize the variable in the Var section? James _______________________________________________ fpc-pascal maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Mattias Gaertner
to SPRL AFOR
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

James Richters
to Mattias Gaertner
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

Earlier message 23 lines
-----Original Message----- From: fpc-pascal On Behalf Of Mattias Gaertner via fpc-pascal Sent: Monday, September 9, 2019 5:59 AM To: Cc: Mattias Gaertner Subject: Re: [fpc-pascal] Illegal counter variable? On Mon, 9 Sep 2019 11:44:05 +0200 SPRL AFOR 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
James Richters
to Mattias Gaertner: On Mon, 9 Sep 2019 11:44:05 +0200 That page only talks about assigning the loop var…
That page only talks about assigning the loop var *inside* the loop, which is forbidden.

You can re-assign the loop variable if you are in {$Mode TP} it works perfectly fine to re-assign the for variable inside the loop, and I use that extensively, I have TP units specifically so I can do that. I really don't understand why it's forbidden if you aren't in TP mode... programmers can take care of their own variables and don't need to be protected from themselves. I have good reasons to want to change the for loop variable and never have a problem with it. I know there are ways around it, but I prefer to just modify the for variable... it can be very powerful.

This works great!

{$MODE TP}
Var
   I:Byte;
Begin
  I:=57;
  For I := I to 100 do
    Begin
       If I=87 then
          I:=95;
       Write(I,' ');
    End;
End.

Output:
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 95 96 97 98 99 100

James

Martin Wynne
to James Richters
James Richters wrote 11 lines
Var I:Byte; Begin I:=57; For I := I to 100 do Begin If I=87 then I:=95; Write(I,' '); End; End.

Why not:

Var
     I:Byte;
Begin
    I:=57;
    For I := I to 100 do
      Begin
         If (I>86) And (I<95) then Continue;
         Write(I,' ');
      End;
End.

which is much easier to follow the logic.

cheers,

Martin.

DougC
to Martin Wynne

The last messages have wandered off topic somewhat.

Can we stick to the original request, which was why the handling of the two declarations differed?

Doug C.

James Richters
to Martin Wynne: Why not: Var I:Byte; Begin I:=57; For I := I to 100 do Begin If (I>86) And (I<95) then…

If (I>86) And (I<95) then Continue;
What does continue do exactly? Loop back to the beginning of the for loop right away? I didn't even know there was such a command.. it's not mentioned here: https://wiki.lazarus.freepascal.org/FOR..DO

only break is mentioned.

As I said, there are ways around it, and that was just a stupid example that proves it works in {$Mode TP} I have some really complicated nested for loops and changing the loop variable is really useful.. also it allows you to skip cycling around in the loop. I just don't see why having the limitation, there is no technical reason that the for loop couldn't change that I can see.. especially since it works in TP mode. Yes, I can work around it.. change to a while loop instead where or something.. where you can change variables all you want... I just don't see the point of enforcing this no-changing-the-for-loop-variable rule...

Pascal doesn't have things like step... but if I modify the for variable myself I can make it act like it had step

{$Mode TP}
Var
   I:Byte;
Begin
  For I := 0 to 100 do
    Begin
       Write(I,' ');
       Inc(I,3);
    End;
End.
0 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 100

Or

{$Mode TP}
Var
   I:Byte;
Begin
  For I := 101 downto 0 do
    Begin
       Dec(I);
       Write(I,' ');
    End;
End.
100 98 96 94 92 90 88 86 84 82 80 78 76 74 72 70 68 66 64 62 60 58 56 54 52 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2 0

Yes lots of ways to do these things... but could always do it with for loops with Pascal before.. and they work fine this way in FPC if you are in TP mode, so why not just allow it all the time?... just to have the flexibility to do what you want. It would just be nice to get everything to work in all my units all the time.. but if I use TP mode, then I can't do things like use case statements with strings.

James

Earlier message 37 lines
-----Original Message----- From: fpc-pascal On Behalf Of Martin Wynne Sent: Monday, September 9, 2019 8:53 AM To: Subject: Re: [fpc-pascal] Illegal counter variable? On 09/09/2019 13:38, James Richters wrote: > Var > I:Byte; > Begin > I:=57; > For I := I to 100 do > Begin > If I=87 then > I:=95; > Write(I,' '); > End; > End. Why not: Var I:Byte; Begin I:=57; For I := I to 100 do Begin If (I>86) And (I<95) then Continue; Write(I,' '); End; End. which is much easier to follow the logic. cheers, Martin.
Bernd Oppolzer
to James Richters
James Richters wrote
I just don't see why having the limitation, there is no technical reason that the for loop couldn't change that I can see.. especially since it works in TP mode.

The original reason why some Pascal implementations had this limitation:

for performance or optimization reasons, the loop control variable was transferred to a register at the beginning of the loop, and changing the variable (at its storage location) inside the loop simply had no effect, because the variable was not fetched from there again during loop execution.
Worse: maybe, to make read accesses to the loop control variable valid inside the loop, they are prepared by storing the control register value into the loop control variable, thus turning changes to the loop control variable useless.

Forbidding (write) accesses to the loop control variable allows for many aggressive optimization strategies around loops.

Maybe today such limitations seem too restrictive.

Kind regards

Bernd

Jonas Maebe
to James Richters: Can someone please tell me why this happens? Var I : Longint = 0; Begin For I := 1 to 6 Do
James Richters wrote 14 lines
Can someone please tell me why this happens? Var I : Longint = 0; Begin For I := 1 to 6 Do Writeln(I); End. This gives me : initialize.pas(4,5) Error: Illegal counter variable initialize.pas(6,4) Fatal: There were 1 errors compiling module, stopping initialize.pas(0) Fatal: Compilation aborted

"var i : longint = 0;" is internally handled using the same code path as "const i : longint = 0", and typed constants cannot be used as counter variables. This is indeed probably a bug.

Jonas

Markus Greim
to Martin Wynne

AFAIK these is the standard behavior since the first PASCAL versions.

We must not change it. It prevents a lot of side effects, and PASCAL is NOT C without brackets! Use while or repeat instead!

From Niklaus Wirths last 2004 Oberon manual:

https://people.inf.ethz.ch/wirth/ProgInOberon2004.pdf​

t "It is recommended that the for statement be used in simple cases only; in particular, no components of the expressions determining the range must be affected by the repeated statements, and, above all, the control variable itself must not be changed by the repeated statements. The value of the control variable must be considered as undefined after the for statement is terminated."

Kind Regards

Markus

Earlier message 32 lines
--- original message --- On September 9, 2019, 5:20 PM GMT+2 wrote: On 09/09/2019 15:11, James Richters wrote: >> If (I>86) And (I<95) then Continue; >> What does continue do exactly? Loop back to the beginning of the for loop right away? > Hi James, > Yes in effect -- it jumps forward to the test at the end of a loop. Very > useful. > See: https://www.freepascal.org/docs-html/rtl/system/continue.html > cheers, > Martin. > _______________________________________________ > fpc-pascal maillist - > https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal --- end of original message ---
James Richters
to Bernd Oppolzer: The original reason why some Pascal implementations had this limitation: for performance…

Yes, today such limitations do seem too restrictive, I wonder if the reasons for the restrictions have become obsolete. You would have to have a really slow computer with very limited resources to optimize loops to the point of reducing functionality like this, and the tendency with modern pc's is to have a larger library of much more powerful and more flexible tools, even if they are more complex and take more memory and resources. For example Case statements used to only work with characters or integers, but the modern version now also works with strings, very much added functionality for sure, but also would use more resources.... but we would all rather have the capability because even a raspberry pi is blazing fast and has ram to burn compared to our old 8086s It seems silly to me that I can't so what I used to do with Turbo Pascal for DOS where I was limited to programs of a few hundred K, due to optimizations that just can't make much of a difference at all on modern computers. I am happy that FPC has TP compatibility mode though! It's just I get to a point where I eventually can't cross units since I can't have a circular unit reference. So I have to choose for any given unit.. do I want my old for loops and change the control variables, or do I want cool new case statements... etc.. it would be nice to have {$Mode everything_the_way_I_want_it}

Since the compiler knows I was trying to change the variable inside the for loop, could it not just compile in not quite as efficient code (TP MODE for loop) when it detects this, and use the more efficient optimized code when it detects that it is able to use it?

James

Earlier message 25 lines
-----Original Message----- From: fpc-pascal On Behalf Of Bernd Oppolzer Sent: Monday, September 9, 2019 10:46 AM To: FPC-Pascal users discussions Subject: Re: [fpc-pascal] Illegal counter variable? Am 09.09.2019 um 16:11 schrieb James Richters: > I just don't see why having the limitation, there is no technical > reason that the for loop couldn't change that I can see.. especially since it works in TP mode. The original reason why some Pascal implementations had this limitation: for performance or optimization reasons, the loop control variable was transferred to a register at the beginning of the loop, and changing the variable (at its storage location) inside the loop simply had no effect, because the variable was not fetched from there again during loop execution. Worse: maybe, to make read accesses to the loop control variable valid inside the loop, they are prepared by storing the control register value into the loop control variable, thus turning changes to the loop control variable useless. Forbidding (write) accesses to the loop control variable allows for many aggressive optimization strategies around loops. Maybe today such limitations seem too restrictive. Kind regards Bernd
Bart
to Jonas Maebe: "var i : longint = 0;" is internally handled using the same code path as "const i :…
PM Jonas Maebe wrote
"var i : longint = 0;" is internally handled using the same code path as "const i : longint = 0", and typed constants cannot be used as counter variables. This is indeed probably a bug.

D7 does not allow this for global variables: "for loop variables must be simple local variables".
In D7 you cannot initialize a local variable, so I cannot test that scenario. Someone with a more recent Delphi should check if this sysnatx is allowed.

Bart
Markus Greim
to Bart

James,

not every body is using a GHz machine. I am , for example, programming a 80186 in an embedded system with very limited speed an RAM.

But I understand thats not a general argument.

But look at MISRA C. Its a big set of rules for "real" save C programming, more or less now the standard in the automobile industry.

Also here are strict rules for counters in for loops.

See:

https://www.misra.org.uk/forum/viewtopic.php?t=272​

So what they are doing is writing Pascal programs in C... ;-) And they need huge and expensive and complex tools to check the C code for

MISRA compatibility!
So we should NOT give up the safety of Pascal for the sloppiness of C!

Kind Regards

Markus

Earlier message 52 lines
--- original message --- On September 9, 2019, 7:19 PM GMT+2 wrote: Yes, today such limitations do seem too restrictive, I wonder if the reasons for the restrictions have become obsolete. You would have to have a really slow computer with very limited resources to optimize loops to the point of reducing functionality like this, and the tendency with modern pc's is to have a larger library of much more powerful and more flexible tools, even if they are more complex and take more memory and resources. For example Case statements used to only work with characters or integers, but the modern version now also works with strings, very much added functionality for sure, but also would use more resources.... but we would all rather have the capability because even a raspberry pi is blazing fast and has ram to burn compared to our old 8086s It seems silly to me that I can't so what I used to do with Turbo Pascal for DOS where I was limited to programs of a few hundred K, due to optimizations that just can't make much of a difference at all on modern computers. I am happy that FPC has TP compatibility mode though! It's just I get to a point where I eventually can't cross units since I can't have a circular unit reference. So I have to choose for any given unit.. do I want my old for loops and change the control variables, or do I want cool new case statements... etc.. it would be nice to have {$Mode everything_the_way_I_want_it} Since the compiler knows I was trying to change the variable inside the for loop, could it not just compile in not quite as efficient code (TP MODE for loop) when it detects this, and use the more efficient optimized code when it detects that it is able to use it? James -----Original Message----- From: fpc-pascal On Behalf Of Bernd Oppolzer Sent: Monday, September 9, 2019 10:46 AM To: FPC-Pascal users discussions Subject: Re: [fpc-pascal] Illegal counter variable? Am 09.09.2019 um 16:11 schrieb James Richters: >> I just don't see why having the limitation, there is no technical >> reason that the for loop couldn't change that I can see.. especially since it works in TP mode. > The original reason why some Pascal implementations had this limitation: > for performance or optimization reasons, the loop control variable was transferred to a register at the beginning of the loop, and changing the variable (at its storage location) inside the loop simply had no effect, because the variable was not fetched from there again during loop execution. > Worse: maybe, to make read accesses to the loop control variable valid inside the loop, they are prepared by storing the control register value into the loop control variable, thus turning changes to the loop control variable useless. > Forbidding (write) accesses to the loop control variable allows for many aggressive optimization strategies around loops. > Maybe today such limitations seem too restrictive. > Kind regards > Bernd > _______________________________________________ > fpc-pascal maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal > _______________________________________________ > fpc-pascal maillist - > https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal --- end of original message ---
Mattias Gaertner
to Bart: D7 does not allow this for global variables: "for loop variables must be simple local…
Show quoted text 12 lines
On Mon, Sep 9, 2019 at 4:54 PM Jonas Maebe wrote: > "var i : longint = 0;" is internally handled using the same code > path as "const i : longint = 0", and typed constants cannot be used > as counter variables. This is indeed probably a bug. D7 does not allow this for global variables: "for loop variables must be simple local variables". In D7 you cannot initialize a local variable, so I cannot test that scenario. Someone with a more recent Delphi should check if this sysnatx is allowed.

Same in 10.3.

Mattias

James Richters
to Mattias Gaertner

Maybe there's no technical reason it doesn't work... it's just set to not work to make it compatible with other pascal programs?

Earlier message 26 lines
-----Original Message----- From: fpc-pascal On Behalf Of Mattias Gaertner via fpc-pascal Sent: Monday, September 9, 2019 1:32 PM To: Cc: Mattias Gaertner Subject: Re: [fpc-pascal] Illegal counter variable? On Mon, 9 Sep 2019 19:27:49 +0200 Bart wrote: > On Mon, Sep 9, 2019 at 4:54 PM Jonas Maebe > wrote: > > > "var i : longint = 0;" is internally handled using the same code > > path as "const i : longint = 0", and typed constants cannot be used > > as counter variables. This is indeed probably a bug. > > D7 does not allow this for global variables: "for loop variables must > be simple local variables". > In D7 you cannot initialize a local variable, so I cannot test that > scenario. Someone with a more recent Delphi should check if this > sysnatx is allowed. Same in 10.3. Mattias
James Richters
to Markus Greim: not every body is using a GHz machine. I am , for example, programming a 80186 in an…

I’m trying to use it in a way that always worked in Turbo Pascal for DOS, I have no idea how to program in C, I find Pascal much more clear and easy to follow and C just confuses me, so I never spent much time other than a few stray projects here and there on C… I’ve always used pascal since the 80s, and in fact my units that change the for loop variable that run in {$Mode TP} are the original code from turbo pascal written in the 80s.. over the decades I’ve modified stuff all around it, but the actual code with for loops with changing control variables is the exact same code I was running on old 4.7MHz Dos machines with turbo pascal for DOS… so for nearly 40 years I have never even known there was some rule against changing the control variable in for loops, it’s always worked and I’ve always used it.. and it still works if I use {$Mode TP} it may not work if I try to use Delphi or something else, and there may be some pascal version even older than turbo pascal for DOS that didn’t act this way, but from the entire scope of my programming experience, this is something that has just always worked and I never had a reason not to use it.

James

Earlier message 78 lines
From: fpc-pascal On Behalf Of Markus Greim Sent: Monday, September 9, 2019 1:32 PM To: FPC-Pascal users discussions Subject: Re: [fpc-pascal] Illegal counter variable? James, not every body is using a GHz machine. I am , for example, programming a 80186 in an embedded system with very limited speed an RAM. But I understand thats not a general argument. But look at MISRA C. Its a big set of rules for "real" save C programming, more or less now the standard in the automobile industry. Also here are strict rules for counters in for loops. See: https://www.misra.org.uk/forum/viewtopic.php?t=272​ So what they are doing is writing Pascal programs in C... ;-) And they need huge and expensive and complex tools to check the C code for MISRA compatibility! So we should NOT give up the safety of Pascal for the sloppiness of C! Kind Regards Markus <https://app.frontapp.com/api/1/noauth/companies/schleibinger_geraete_gmbh/seen/msg_3t3yn9e/han_9pzu6a/a2c200f4.gif> On September 9, 2019, 7:19 PM GMT+2 wrote: Yes, today such limitations do seem too restrictive, I wonder if the reasons for the restrictions have become obsolete. You would have to have a really slow computer with very limited resources to optimize loops to the point of reducing functionality like this, and the tendency with modern pc's is to have a larger library of much more powerful and more flexible tools, even if they are more complex and take more memory and resources. For example Case statements used to only work with characters or integers, but the modern version now also works with strings, very much added functionality for sure, but also would use more resources.... but we would all rather have the capability because even a raspberry pi is blazing fast and has ram to burn compared to our old 8086s It seems silly to me that I can't so what I used to do with Turbo Pascal for DOS where I was limited to programs of a few hundred K, due to optimizations that just can't make much of a difference at all on modern computers. I am happy that FPC has TP compatibility mode though! It's just I get to a point where I eventually can't cross units since I can't have a circular unit reference. So I have to choose for any given unit.. do I want my old for loops and change the control variables, or do I want cool new case statements... etc.. it would be nice to have {$Mode everything_the_way_I_want_it} Since the compiler knows I was trying to change the variable inside the for loop, could it not just compile in not quite as efficient code (TP MODE for loop) when it detects this, and use the more efficient optimized code when it detects that it is able to use it? James -----Original Message----- From: fpc-pascal < > On Behalf Of Bernd Oppolzer Sent: Monday, September 9, 2019 10:46 AM To: FPC-Pascal users discussions < > Subject: Re: [fpc-pascal] Illegal counter variable? Am 09.09.2019 um 16:11 schrieb James Richters: > I just don't see why having the limitation, there is no technical > reason that the for loop couldn't change that I can see.. especially since it works in TP mode. The original reason why some Pascal implementations had this limitation: for performance or optimization reasons, the loop control variable was transferred to a register at the beginning of the loop, and changing the variable (at its storage location) inside the loop simply had no effect, because the variable was not fetched from there again during loop execution. Worse: maybe, to make read accesses to the loop control variable valid inside the loop, they are prepared by storing the control register value into the loop control variable, thus turning changes to the loop control variable useless. Forbidding (write) accesses to the loop control variable allows for many aggressive optimization strategies around loops. Maybe today such limitations seem too restrictive. Kind regards Bernd
Unknown author
to James Richters: If (I>86) And (I<95) then Continue; What does continue do exactly? Loop back to the…
James Richters wrote
Pascal doesn't have things like step...

hunh??? i don't think that's right but i'm just catching up after several 10+ hours days of $job...

i know that i've written code in the past that did use something to step X numbers per run through the look and it did not involved manually incrementing the loop var...

NOTE: No off-list assistance is given without prior approval. *Please keep mailing list traffic on the list unless* *a signed and pre-paid contract is in effect with us.*
Ralf Quint
to Unknown author
Show quoted text 11 lines
On 9/9/19 10:11 AM, James Richters wrote: > Pascal doesn't have things like step... hunh??? i don't think that's right but i'm just catching up after several 10+ hours days of $job... i know that i've written code in the past that did use something to step X numbers per run through the look and it did not involved manually incrementing the loop var...

I am not aware of any Pascal implementation that does have a STEP parameter for FOR loops, and I am programming in Pascal at least as long as you... ;-)

The closest would be the FOR..IN variant, which works in Delphi (D2006? and later) and is AFAIK supported in FPC by now. But that would be iterating over a set, not increment an integer/cardinal variable in an increment other than 1...

Ralf

Christo Crause
to Ralf Quint
AM Ralf Quint wrote 16 lines
On 9/10/2019 4:26 PM, wrote: > On 9/9/19 10:11 AM, James Richters wrote: >> Pascal doesn't have things like step... > > hunh??? i don't think that's right but i'm just catching up after > several 10+ hours days of $job... > > i know that i've written code in the past that did use something to > step X numbers per run through the look and it did not involved > manually incrementing the loop var... > > I am not aware of any Pascal implementation that does have a STEP parameter for FOR loops, and I am programming in Pascal at least as long as you... ;-)

FPC documentation:
https://www.freepascal.org/docs-html/ref/refsu58.html#x164-18600013.2.4 - at least FPC doesn't allow specifying an increment size. Several other languages do allow specifying an increment (nice list of for loop constructs with specified increment in various languages: https://www.rosettacode.org/wiki/Loops/For_with_a_specified_step). Modula-2 or Oberon-2 code snippets could perhaps be mistaken for Pascal through the mists of time.

Sven Barth
to Christo Crause

Christo Crause schrieb am Mi., 11. Sep. 2019,
09:54:

Show quoted text 29 lines
On Wed, Sep 11, 2019 at 7:06 AM Ralf Quint wrote: > On 9/10/2019 4:26 PM, wrote: > > On 9/9/19 10:11 AM, James Richters wrote: > >> Pascal doesn't have things like step... > > > > hunh??? i don't think that's right but i'm just catching up after > > several 10+ hours days of $job... > > > > i know that i've written code in the past that did use something to > > step X numbers per run through the look and it did not involved > > manually incrementing the loop var... > > > > > I am not aware of any Pascal implementation that does have a STEP > parameter for FOR loops, and I am programming in Pascal at least as long > as you... ;-) > FPC documentation: https://www.freepascal.org/docs-html/ref/refsu58.html#x164-18600013.2.4 - at least FPC doesn't allow specifying an increment size. Several other languages do allow specifying an increment (nice list of for loop constructs with specified increment in various languages: https://www.rosettacode.org/wiki/Loops/For_with_a_specified_step). Modula-2 or Oberon-2 code snippets could perhaps be mistaken for Pascal through the mists of time.

Well, we do have a patch for "for ... (down)to ... by" support: https://bugs.freepascal.org/view.php?id=25549

Regards,
Sven

Martin Wynne
to Martin Wynne
Show quoted text 7 lines
If step is wanted, it's easy enough: For n:=a to b Do   Begin     if n Mod step 0 then Continue;     ...

p.s. make that

   if (n-a) Mod step  0 then Continue;

for cases where a is not a multiple of step.

Martin.

James Richters
to Sven Barth: Christo Crause schrieb am Mi., 11. Sep. 2019, 09:54: Well, we do have a patch for "for…

How do you get ‘By’ to work? I have downto working but if I try ‘By’ I get

Fatal: Syntax error, "DO" expected but "identifier BY" found

If FPC is going to support downto and by, why not also support changing the control variable when not in {$Mode TP} as well, and just make a note that doing so may not be compatible with other pascal compilers.. but who cares anyway FPC is better than all the others anyway 😊

Well, we do have a patch for "for ... (down)to ... by" support: https://bugs.freepascal.org/view.php?id=25549

James

James Richters
to Unknown author: hunh??? i don't think that's right but i'm just catching up after several 10+ hours days…

I don't know what the syntax would be if it did have a way to do a step

https://wiki.lazarus.freepascal.org/FOR..DO

"In Pascal, the for loop can only count in increments (steps) of 1"

James

Earlier message 18 lines
-----Original Message----- From: fpc-pascal On Behalf Of Sent: Tuesday, September 10, 2019 7:26 PM To: Subject: Re: [fpc-pascal] Illegal counter variable? On 9/9/19 10:11 AM, James Richters wrote: > Pascal doesn't have things like step... hunh??? i don't think that's right but i'm just catching up after several 10+ hours days of $job... i know that i've written code in the past that did use something to step X numbers per run through the look and it did not involved manually incrementing the loop var... -- NOTE: No off-list assistance is given without prior approval. *Please keep mailing list traffic on the list unless* *a signed and pre-paid contract is in effect with us.* _______________________________________________
Sven Barth
to James Richters: How do you get ‘By’ to work? I have downto working but if I try ‘By’ I get…

James Richters schrieb am Mi., 11. Sep.
2019, 16:24:

Show quoted text 7 lines
How do you get ‘By’ to work? I have downto working but if I try ‘By’ I get Fatal: Syntax error, "DO" expected but "identifier BY" found

Did you see anywhere that said that this bug report was resolved and the patch integrated into trunk?

Show quoted text 6 lines
If FPC is going to support downto and by, why not also support changing the control variable when not in {$Mode TP} as well, and just make a note that doing so may not be compatible with other pascal compilers.. but who cares anyway FPC is better than all the others anyway 😊

There is no interest in lifting that restriction. Out of the dialects that FPC supports TP is the only one that does not. Even Wirth advised against allowing it.

Also FPC already supports downto. Just like TP and Delphi do...

Regards,
Sven

DougC
to Unknown author

Actually, the original code used two VARs, not typed CONST. One VAR was initialized and one was not. I think this is a bug and should be fixed.

As Jonas already pointed out:

"var i : longint = 0;" is internally handled using the same code path as

"const i : longint = 0", and typed constants cannot be used as counter

variables. This is indeed probably a bug.

---- On Wed, 11 Sep 2019 12:40:57 -0400 wrote ----

Yes, off topic. The initial poster's code had the loop variable declared as an initialized constant. Not as a true variable. No FPC in hand, I can't test if setting 'assignable typed constants = true' would permit the use of an initialized typed constant or not. 

James Richters
to DougC

I reported it here:

https://bugs.freepascal.org/view.php?id=36065

James

Actually, the original code used two VARs, not typed CONST. One VAR was initialized and one was not. I think this is a bug and should be fixed.
As Jonas already pointed out:
Show quoted text 5 lines
"var i : longint = 0;" is internally handled using the same code path as "const i : longint = 0", and typed constants cannot be used as counter variables. This is indeed probably a bug.
---- On Wed, 11 Sep 2019 12:40:57 -0400 < > wrote ----
Yes, off topic. The initial poster's code had the loop variable declared as an initialized constant. Not as a true variable. No FPC in hand, I can't test if setting 'assignable typed >constants = true' would permit the use of an initialized typed constant or not.
Unknown author
to Ralf Quint: I am not aware of any Pascal implementation that does have a STEP parameter for FOR…
Ralf Quint wrote 14 lines
On 9/10/2019 4:26 PM, wrote: > On 9/9/19 10:11 AM, James Richters wrote: >> Pascal doesn't have things like step... > > hunh??? i don't think that's right but i'm just catching up after several 10+ > hours days of $job... > > i know that i've written code in the past that did use something to step X > numbers per run through the look and it did not involved manually incrementing > the loop var... > > I am not aware of any Pascal implementation that does have a STEP parameter for FOR loops, and I am programming in Pascal at least as long as you... ;-)

i may be thinking of a while loop... am in the middle of 10+ hour days at the moment... first since May of this year... at least now i don't have to go to the food bank for eats... maybe this weekend i can scan my old TP6 code and find what i'm remembering?? i dunno...

NOTE: No off-list assistance is given without prior approval. *Please keep mailing list traffic on the list unless* *a signed and pre-paid contract is in effect with us.*