Recent

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

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #15 on: July 09, 2012, 03:55:58 pm »
As can be seen, "There's more than one way to skin a cat".

So I'll add mine, personally I think @envy's solution is best, but it you wanted to keep your FOR-LOOP construct you can do this.

Code: [Select]
procedure Test;
var
  i:integer;
begin
  for i:=0 to 100  do
  begin
    case i of 5..9: continue; end;
    writeln(i);
  end;
end;


 

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #16 on: July 09, 2012, 04:02:28 pm »
"There's more than one way to skin a cat".  Haha!  I got in so much trouble with that line and a "cat fanatic"  :)  Only I said "There's more than one way to skin kittens".

Anyway, I wish they had a "Like" button for Posts.  Your code looks the cleanest and easiest to read.
Lazarus Trunk / fpc 2.6.2 / Win32

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #17 on: July 09, 2012, 04:25:56 pm »
Quote
Haha!  I got in so much trouble with that line and a "cat fanatic"

:)  Well there appears to be a few idea's of were this expression came from,  here in the UK it's a very common saying.  So my gut feeling it's origin was this one explained here ->
http://wiki.answers.com/Q/What_is_the_origin_for_the_saying_there's_more_than_one_way_to_skin_a_cat

So IOW, were these "Cat Fanatics" actually "Whip Fanatics",  --- "ooh matron".. :)

Btw.  I own two cats, not really thought about skinning them, although my wife might think about it every time one misses the litter tray. :)

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #18 on: July 09, 2012, 04:47:41 pm »
It's very common in the US as well, only I think they really mean "skin a cat".  A holdover from the days when trappers made their living selling/trading animal pelts.

The code that I gave was just something I did quickly without much thought to try and help the poster understand that he really shouldn't try to interfere with the loop counter of a For Loop.  Your code looked like you took the time to think it through.
« Last Edit: July 09, 2012, 06:09:24 pm by Avishai »
Lazarus Trunk / fpc 2.6.2 / Win32

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #19 on: July 09, 2012, 09:33:50 pm »
Or even...
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var i, i2: integer;
begin
  i:=0;
  for i2:=0 to 100-5 do begin
    if i2=5 then i:=10;
    {
    Do something
    }
    inc(i);
  end;
end;

I like the while loop most though.
« Last Edit: July 09, 2012, 09:35:22 pm by User137 »

CM630

  • Hero Member
  • *****
  • Posts: 1641
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #20 on: August 08, 2012, 09:55:37 am »
Yet another odd case occured?
I cannot find how to set the step (uincrement) for the for loops?
For example I want to run

Code: [Select]
var
  i:double;
begin
  for i:=0 to PI do
  begin

  end;
end;
First I get an Error: Ordinal expression expected.
Does it mean that in Lazarus only integer counters can be used in the for loops?
Лазар 4,4 32 bit (sometimes 64 bit); FPC3,2,2

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #21 on: August 08, 2012, 10:00:03 am »
Quote
Does it mean that in Lazarus only integer counters can be used in the for loops?
Yes, only integers or other ordinal types (char, enumeration).
No floating point is allowed.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #22 on: August 09, 2012, 12:09:43 am »
Quote
Yet another odd case occured?
Nope, floating point is Pascal is a representation of real numbers in math. What do you think the next number after 0.1? The purpose of for loop is to enumerate each element from a given range set, which you can't do with real numbers (open up your math book if you forget).

CM630

  • Hero Member
  • *****
  • Posts: 1641
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #23 on: August 09, 2012, 08:11:36 am »
Leledumbo, I cannot understand what you are trying to say, unless you mean that the for loops are meant primarily for indexing array elements.

Here is an example in the MS Office VB:
Code: [Select]
  For i = 0 To 2 Step 0.1
     MsgBox (i)
  Next i
The next number after 0,1 is 0,2 and the previous one is 0.

Also, I suppose that I cannot run this is Lazarus?
Code: [Select]
 
  For i = 1 To 10 Step 2
     MsgBox (i)
  Next i
Лазар 4,4 32 bit (sometimes 64 bit); FPC3,2,2

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #24 on: August 09, 2012, 10:50:44 am »
Main difference between Basic and Pascal is that Basic is not compiled language. Simply, your CPU has instructions for increment and decrement by one and it is the fastest.

Your code in Pascal:
Code: [Select]
For i := 0 To 20 do
     WriteLn (floattostr(i/10));
// or ShowMessage (floattostr(i/10));
Code: [Select]
For i := 1 To 5 do
     WriteLn (inttostr(2*i));
// or ShowMessage (inttostr(2*i));
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #25 on: August 09, 2012, 05:55:03 pm »
Quote
Leledumbo, I cannot understand what you are trying to say, unless you mean that the for loops are meant primarily for indexing array elements.
OK, that's fine. Perhaps you have pure programming background (probably with languages without strong scientific background), Pascal is designed with heavy math influence. For loop is generic, its primary use is to loop in a predictable way (e.g. you can make sure it has to and will terminate).
Quote
Here is an example in the MS Office VB
That's something not available in Pascal (nor in subsequent Wirthian languages). For loop can be optimized aggressively by the compiler when it follows certain rules, like one that Blaazen has pointed out. That VB "feature" makes some of the optimizations harder or impossible.

Knipfty

  • Full Member
  • ***
  • Posts: 232
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #26 on: August 09, 2012, 06:22:50 pm »
Paskal,

You may want to check out this webpage http://www.freepascal.org/docs-html/ref/refsu49.html#x141-15100013.2.4

The take away from this is "The control variable must be an ordinal type, no other types can be used as counters in a loop."

Also,
Code: [Select]
The following will generate an error:
For I:=0 to 100 do 
  begin 
  DoSomething; 
  I:=I*2; 
  end;

because the loop variable I cannot be assigned to inside the loop.


Visual Basic make a lot os use around the variant type.  The upside to this type (which is avaialble in Free Pascal), is that variables can be anything at anytime.  The downside is that these variable types are large (I seem to rememebr them being somehting like 20 bytes) and slow.  The program has to do a lot of work in handling them.

I hope that helps,

Knipfty
« Last Edit: August 09, 2012, 07:03:56 pm by Knipfty »
64-bit Lazarus 2.2.0 FPC 3.2.2, 64-bit Win 11

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #27 on: August 09, 2012, 07:17:41 pm »
I see this not mentioned in this thread yet, so...

Code: [Select]
var i: integer;
...
  for i in [0..4, 11..100] do begin
    { Do something with i }
  end;

eny

  • Hero Member
  • *****
  • Posts: 1657
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #28 on: August 09, 2012, 07:37:53 pm »
All posts based on: Win11; Lazarus 4_4  (x64) 12-02-2026 (unless specified otherwise...)

Knipfty

  • Full Member
  • ***
  • Posts: 232
Re: Strange: Error: Illegal assignment to for-loop variable "i"
« Reply #29 on: August 09, 2012, 08:22:14 pm »
user137,

What would the difference be between what you wrote and

Code: [Select]
    if i in [0..4, 11..100] then
    begin
      { Do something with i }
    end;

Knipfty
64-bit Lazarus 2.2.0 FPC 3.2.2, 64-bit Win 11

 

TinyPortal © 2005-2018