[fpc-pascal] Illegal counter variable?
James Richters
james at productionautomation.net
Mon Sep 9 16:11:11 CEST 2019
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
-----Original Message-----
From: fpc-pascal <fpc-pascal-bounces at lists.freepascal.org> On Behalf Of Martin Wynne
Sent: Monday, September 9, 2019 8:53 AM
To: fpc-pascal at lists.freepascal.org
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.
_______________________________________________
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