Vadim Skormakhovich
Vadim Skormakhovich

Reputation: 125

Is there any way to loop through range with for/in in delphi?

type
  TRange = MIN_VALUE..MAX_VALUE;

Is there any way to loop through this range using for/in loop?

for rangeValue in TRange do 
  smth

Or should i loop through it like that:

for rangeValue := Low(TRange) to High(TRange) do
  smth

Upvotes: 2

Views: 504

Answers (2)

Paul McGee
Paul McGee

Reputation: 104

As of 2022 ... This works in at least 10.2 onwards ...

program Project1;
{$APPTYPE CONSOLE}

type
  TRange = 0..9;

begin
  for var rangeValue in [low(TRange)..high(TRange)] do write('*');

  readln;
end.

but what you asked for works right now in FPC ... ( https://godbolt.org/z/9zrYb9xY7 )

program output;

type
  TRange = 0..9;
var 
  rangeValue : integer;

begin
  for rangeValue in TRange do write('*');
end.

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 613302

It is not possible to use for/in loops with subrange types. You will have to use the traditional for loop syntax.

Upvotes: 3

Related Questions