Recent

Author Topic: Strange: Error: Illegal assignment to for-loop variable "i"  (Read 48767 times)

CM630

  • Hero Member
  • *****
  • Posts: 1641
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
I have the following code:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  i:integer;
begin
    for i:=0 to 100  do
    begin
       if i=5 then i:=10;
    end;
end;

When I try to run it I get Error: Illegal assignment to for-loop variable "i"
Is this a bug in Lazarus or I am doing something wrong?
Лазар 4,4 32 bit (sometimes 64 bit); FPC3,2,2

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #1 on: July 09, 2012, 07:31:57 am »
I think you're not supposed to change the counter/control variable in a for loop - i in this case.

If you need to, try one of the alternatives (a while or until loop - don't know by heart if you are allowed to change the control variable there)... or just test for i=5,6,7,8 and 9 and skip processing to the next iteration of the loop if necessary.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #2 on: July 09, 2012, 08:26:49 am »
For loop is supposed to be a fixed iteration, the termination should be predictable. If you're used to Turbo Pascal, AFAIK they violate this so it's allowed. In Turbo Pascal compatibility mode (and perhaps Delphi mode as well), the restriction is removed so it's allowed as in Turbo Pascal.

CM630

  • Hero Member
  • *****
  • Posts: 1641
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #3 on: July 09, 2012, 10:26:59 am »
How am I to switch to Turbo Pascal compatibility mode?
I tried with adding -Mtp in Project/Project Options/Linking/Options (-k):/
I checked the checkbox Pass options to linker, too.

I tried to run the code, but I got the same error?

I then uncommented a line #  -Mtp in fpc.cfg and I still get the same.
Uncommenting # Mdelphi gave no result, too.
« Last Edit: July 09, 2012, 10:48:39 am by paskal »
Лазар 4,4 32 bit (sometimes 64 bit); FPC3,2,2

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #4 on: July 09, 2012, 10:53:37 am »
You've done it right, but if the source file contains:
Code: [Select]
{$mode objfpc}Then it would be preferred over the -Mtp as explained here. Just remove that from the source file, note that if you're on Turbo Pascal compatibility mode, you're coding as in Turbo Pascal. i.e. no classes, no overloading, and many other modern Pascal features (read the docs). A better solution is to change your code not to use for loop because as I said earlier, it's a violation against the semantic of for loop.

Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #5 on: July 09, 2012, 10:56:02 am »
I have the following code:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  i:integer;
begin
    for i:=0 to 100  do
    begin
       if i=5 then i:=10;
    end;
end;

When I try to run it I get Error: Illegal assignment to for-loop variable "i"
Is this a bug in Lazarus or I am doing something wrong?


Paskal,
Seems like in your code, nothing gets done when i is from 5 to 9. So you could put code to that effect (do nothing when i  is any of 5 to 9).

But correct me if that's not what you want to happen.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #6 on: July 09, 2012, 10:59:20 am »
To add to what leldumbo wrote - any {$mode ...} directive in your source will override commandline directives. So to force Turbo Pascal mode you would have to use
Code: [Select]
{$MODE TP}

eny

  • Hero Member
  • *****
  • Posts: 1657
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #7 on: July 09, 2012, 11:17:38 am »
Better keep it simple:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var cnt:integer;
begin
  cnt := 0;
  repeat
    // do your thing...

    // Skip 6..9
    if cnt = 5
      then cnt := 10
      else inc(cnt);
  until cnt = 101;
end;

And better not use magic numbers: http://en.wikipedia.org/wiki/Magic_number_%28programming%29#Unnamed_numerical_constants
All posts based on: Win11; Lazarus 4_4  (x64) 12-02-2026 (unless specified otherwise...)

CM630

  • Hero Member
  • *****
  • Posts: 1641
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #8 on: July 09, 2012, 01:28:37 pm »
I tried with {$MODE Delphi} but there is no change. Using TP mode makes no sense, IMHO DOS is dead.
I am fully aware, that I can add an extra counter or use another type of loop, and yet I see no reason for this limitation, so I wonder if it is not a bug.
Does anyone have an idea if there is such a limitation in Delphi?

Elmug, my idea is to remove repeating values for an array/ string grid, etc, I just gave a simpler example.
« Last Edit: July 09, 2012, 01:31:17 pm by paskal »
Лазар 4,4 32 bit (sometimes 64 bit); FPC3,2,2

Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #9 on: July 09, 2012, 01:39:11 pm »
I tried with {$MODE Delphi} but there is no change. Using TP mode makes no sense, IMHO DOS is dead.
I am fully aware, that I can add an extra counter or use another type of loop, and yet I see no reason for this limitation, so I wonder if it is not a bug.
Does anyone have an idea if there is such a limitation in Delphi?

Elmug, my idea is to remove repeating values for an array/ string grid, etc, I just gave a simpler example.

Hi Paskal,

Just the thought that changing to downwards compatibility might affect something else, would lead me to rather use code as eny suggests, in which you create your own counter, that you can control.

Surely there must be a reason for the way it is now, otherwise, why would they go to the extra work of not using what was there already, wouldn't you say?

But maybe there isn't.

I hope all resolves well there.

eny

  • Hero Member
  • *****
  • Posts: 1657
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #10 on: July 09, 2012, 01:51:24 pm »
I am fully aware, that I can add an extra counter or use another type of loop, and yet I see no reason for this limitation, so I wonder if it is not a bug.
There is a reason why FP is called a 3GL or HLL: it is easier to develop programs that are easy(ier) to read and understand.
A for control structure is used to execute a statement a fixed number of times (i.e. from a start value to an end value).
This is by design, not a limitation or bug as you call it: http://www.freepascal.org/docs-html/ref/refsu49.html#x141-15100013.2.4

The while and repeat/until give the flexibility for the level of control you seek.

« Last Edit: July 09, 2012, 02:01:12 pm by eny »
All posts based on: Win11; Lazarus 4_4  (x64) 12-02-2026 (unless specified otherwise...)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #11 on: July 09, 2012, 02:20:21 pm »
As an addition to what eny said: a fixed iteration loop has a chance to be optimized more than any other loop. For instance: loop unrolling. It relies on the fact that the iteration is fixed (even more, the number of iteration must be constant, instead of just predictable). If you read up compiler optimizations article at wikipedia, you'll see that a lot of optimizations can be applied to such for-loop.

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #12 on: July 09, 2012, 02:24:13 pm »
Does anyone have an idea if there is such a limitation in Delphi?

Delphi 2007 & XE2  -  [DCC Error] Project4.dpr(14): E2081 Assignment to FOR-Loop variable 'i'

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #13 on: July 09, 2012, 03:18:50 pm »
If you are trying to skip 5 through 10, try this code:

Procedure Tform1.Button1click(Sender: Tobject);
var
  I: Integer;
Begin
  for I:= 0 to 100 do begin
    if (I>4)and(I<11) then
      {Do nothing}
    else begin
      {Add your code here to do what you need}
    End;
  End;
End;
« Last Edit: July 09, 2012, 03:23:48 pm by Avishai »
Lazarus Trunk / fpc 2.6.2 / Win32

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #14 on: July 09, 2012, 03:32:24 pm »
Or you could do something like:

Procedure Tform1.Button2click(Sender: Tobject);
var
  I: Integer;
Begin
  I:= 0;
  repeat
    case I of
      0..4, 11..100: begin
        ListBox1.Items.Add(IntToStr(I));  //whatever your code should be
      End;
    End;
    inc(I);
  Until I>100;
End;
Lazarus Trunk / fpc 2.6.2 / Win32

 

TinyPortal © 2005-2018