Tell me about Free Pascal compiler error 3208 "Illegal assignment to for-loop variable."
Asked
Active
Viewed 684 times
-2
-
Questions you write that you intend to answer yourself must meet the same quality guidelines as any other question. This one is lacking in any detail. It should at a minimum include the code that caused this error to be shown. – Ken White Dec 11 '20 at 00:28
-
Actually it depends on mode. In $mode TP, assigning the loop var is allowed, like in TP. Therefore I voted to reopen. – Marco van de Voort Dec 11 '20 at 11:13
1 Answers
1
We cannot change the value of the indexer variable in a For-In loop.
For demonstration, copy this code into a new Project of type Simple Program, and compile it. Then, add the '$' character to the "Define symDemo" comment in the first line, to convert it to a compiler directive, and try to compile.
For discussion see https://forum.lazarus.freepascal.org/index.php?topic=17488.0
program ForIn; {$AppType Console} { Define symDemo}
var
strChrLst , // list of characters, without separators
idxChrOne : string ; // only one character; indexer in For-In loop
strChrUpr : string = ''; // uppercase version of the current character, init-ed to empty
begin
strChrLst := 'a[z' ; // initialize list of characters to work on
for idxChrOne in strChrLst do begin ; // scan all characters in the list
{$IfNDef symDemo} ; // when "symDemo" is not defined
strChrUpr := UpCase( idxChrOne ) ; // convert the current character to upcase
{$Else} ; // when "symDemo" is defined
idxChrOne := UpCase( idxChrOne ) ; // gives Error 3208 on "idxChrOne"
{$EndIf} ; // done with "symDemo"
if ( strChrUpr < 'A' ) // when the current character is
Or ( strChrUpr > 'Z' ) then begin ; // outside the range of alphabet letters
writeln(StdErr,'Not a letter: ',idxChrOne); // emit the non-letter character on StdErr
end ; // done with the non-letter character
end ; // done scanning characters in the list
end.

Bilbo
- 358
- 1
- 10