Illegal Counter Variable in For Loop if initialized in variable declaration
Original Reporter info from Mantis: Zaaphod
-
Reporter name:
Original Reporter info from Mantis: Zaaphod
- Reporter name:
Description:
Initializing the control variable in the variable declaration as in the following example causes a compiler error:
Error: Illegal counter variable
Var
I:Byte=0;
Begin
For I := 1 to 100 do
Begin
Write(I,' ');
End;
End.
Steps to reproduce:
Try to run this program:
Var
I:Byte=0;
Begin
For I := 1 to 100 do
Begin
Write(I,' ');
End;
End.
It should just work, but it gives a compiler error:
Error: Illegal counter variable
It should be functionally equivalent to:
Var
I:Byte;
Begin
I:=0;
For I := 1 to 100 do
Begin
Write(I,' ');
End;
End.
Which runs fine.
Additional information:
Control variables should not be required to be dedicated to a for loop.
The programmer should be free to re-cycle the control variable if they wish and write a program like this:
Var
I:Byte=0;
Begin
While I < 5 do
Begin
Inc(I);
Writeln(I);
End;
I := Pos('Q','ABCDEFGHIJKLMNOPQRSTUVWXYZ');
Writeln('I=',I);
For I := 1 to 100 do
Begin
Write(I,' ');
End;
Writeln;
I:=67;
Repeat
Write(I,' ');
Dec(I);
Until I<=32;
End.
but this also raises the error: Error: Illegal counter variable, however the variable is completely legitimate, initializing it in the VAR declaration with ALL the other variables should be allowed, even though initializing a control variable really serves no purpose, it should be allowed anyway... There are bad consequences to not initializing variables, and it is good programming to ALWAYS initialize ALL variables whether they need it or not. and if 99.9% of your variables are initialized in the VAR declaration it does not make sense to be required to make an exception to initialize a shared variable that will be eventually used in a for loop elsewhere.
Mantis conversion info:
- Mantis ID: 36065
- OS: Windows
- OS Build: 7 & 10
- Platform: i386
- Version: 3.3.1