for var i:=2 to round(sqrt(n)) do
That "feature" has been discussed many times and it was decided against it. (imho for all the good reasons
Not because it is hard to implement, but because of other reasons:
- The strict separation of declaration and actual use is deemed more important and more in line with true Pascal Philosophy
- For example it makes it easy to introduce hard to find bugs
- It also makes the program less readable to others: you are more or less hiding the use of a variable inside its implementation.
To summarize, although this C style "feature" seems attractive to some, it actually hurts the Pascal style of readability and separation.
Note FreePascal has many, many modern features that are unique. And many are way more useful.
But if somebody cares to implement it!, there is a slight change it could be accepted under a modeswitch that is OFF by default.
That has been discussed too, if I remember correctly.
As an aside: personally I think the feature has the wrong syntax: from a language point of view there should be no declaration at all, because assignments to e.g. loop variables can use type inference like:
for i:=2 to round(sqrt(n)) do // "i" can be type inferred from its use as an ordinal type and its scope is also known
Not in (easily) in this case, but in other cases even the range of the ordinal can be determined, both probably not in LL(1), which is also a Pascal feature/property.
So if one implements it, it is better to skip the "var" at all, it is useless. (And IMNSHO thoughtlessly implemented!! to look like C.)
That is a better still solution but
I am just as against it as using var syntax in loop implementations.
To summarize the next is possible, but not recommended:
//non-working demo code
for i:=2 to round(sqrt(n)) do // inferred ordinality "var" is superfluous
begin
if not (i in primes) then
continue;
x := i*i; // inferred ordinality: "var" is superfluous
while x<=n do
begin
Exclude(primes,x);
x += i;
end;
end;
Note that a similar mechanism is already used in the
for in do, where the indexing ordinal is even completely hidden and only values are used..