Forum > General

Using fields of a record as control variable loop

(1/3) > >>

simone:
Consider this simple program:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1;type  TMyRect=record    X,Y : integer;  end;var  T : TMyRect;begin  for T.X:=1 to 10 do    writeln(T.X);end.
The compiler produces the following error: "project1.lpr(9,8) Error: Illegal counter variable".

Is this expected behavior? If so, what is the reason why it is not possible to use the field of a record as control variable in a for loop?

Thanks for explanations.

egsuh:
I think only local variables are allowed as index.

Arioch:
Perhaps because compiler wants a total control over the placement and lifetime of that variable.

It could end being CPU register, with no memory address at all, and only during the loop span, immediately destroyed after the loop ends.

You can try


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  ...  i: integer absolute T.X;...  for i := ... 
Would probably be equally denied.

P.S. frankly, this "loop variable only for loop itself" approach contradicts with Wirth's "clearly designated section for all-the-variables to be declared in"  approach.

Less structured C approach, where every begin-end can have their own variables, is much more fitting this "for loop" design.
It is typical there to declare yet another variable inside loop, like


--- Code: C  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---int foo( const int bar ){  int i = 20, j = 10;    for(int i = j; i < 100; ++i, j += bar )    {        // do something   }  return i;} 
And recent Delphi given up on this. Notice the "loops" secton in https://blog.marcocantu.com/blog/2018-october-inline-variables-delphi.html

440bx:

--- Quote from: simone on September 27, 2022, 12:50:52 pm ---Is this expected behavior? If so, what is the reason why it is not possible to use the field of a record as control variable in a for loop?

--- End quote ---
Yes, it is expected behavior and it is rooted in the fact that a variable used as the index in a "for" loop is not allowed to be modified inside the loop.

If the compiler allowed using global variables as indexes it would have no way to ensure the index is not modified by some other code (particularly in a multi-threaded program.)

Because of this, index variables should be local variables (outside the reach of other program code.)

HTH.

Arioch:

--- Quote from: 440bx on September 27, 2022, 01:45:40 pm ---local variables (outside the reach of other program code.)

--- End quote ---

which T perfectly was...

For the purposes program's global "begin-end" block - the global variables are local, otherwise this would not compile, and it did compile.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1;   procedure local;  var local_var: record x: integer end;      local_var_y: integer absolute local_var.x;  begin    local_var.x := 10;    local_var_y := 20;     // project1.lpr(11,9) Error: Illegal counter variable    // for local_var_y := 1 to 2 do Writeln(y);    // for local_var.x := 1 to 2 do Writeln(y);  end; var global_var: integer; // global vars are perfectly okay for global loopsbegin  for global_var := 0 to 5 do writeln(global_var);  readln;  local;  readln;end.

Navigation

[0] Message Index

[#] Next page

Go to full version