Example code : Using for..in to loop on elements of an array in a sequence
|
procedure TForm1.W3Button7Click(Sender: TObject);
var
numbers: array of Integer;
a, b : Integer;
begin
numbers.SetLength(4); //SetLength(numbers, 4);
numbers[0] := 20;
numbers[1] := 10;
numbers[2] := 5;
numbers[3] := 1;
for b in numbers do
begin
Inc(a);
WriteLn('Iteration = '+ IntToStr(a) + 'value = '+ IntToStr(b));
end;
end;
|
|
Iteration = 1value = 20
Iteration = 2value = 10
Iteration = 3value = 5
Iteration = 4value = 1
Example code : Using for..in to loop on Const elements
|
const
values: array[0..3] of Integer = (20,5,10,1);
procedure TForm1.W3Button8Click(Sender: TObject);
var
a, b : Integer;
begin
a := 0;
for b in values do
begin
a := a + 1;
WriteLn('Iteration = '+ IntToStr(a) + 'value = '+ IntToStr(b));
end;
end;
|
|
Iteration = 1value = 20
Iteration = 2value = 5
Iteration = 3value = 10
Iteration = 4value = 1
Example code : Using for..in to loop on elements of an array in a sequence
|
type
TSuit = (Hearts = 1, Clubs, Diamonds = 10, Spades);
procedure TForm1.W3Button10Click(Sender: TObject);
var
Suit: TSuit;
number : set of TSuit;
begin
number := [Hearts, Clubs, Diamonds];
for Suit in number do
WriteLn(Ord(Suit));
end;
|
|
1, 2, 10
Example code : Using for..in to loop on elements of an array in a sequence
|
type
TSuit = (Hearts = 1, Clubs, Diamonds = 10, Spades);
procedure TForm1.W3Button11Click(Sender: TObject);
var
suit: TSuit;
suitEnum: array [1..4] of TSuit;
i : Integer = 0;
begin
//initialization
suitEnum[1] := Hearts;
suitEnum[2] := Clubs;
suitEnum[3] := Diamonds;
suitEnum[4] := Spades;
// nonconsecutive enumeration
for suit in suitEnum do
begin
i := i + 1;
WriteLn('Myenum'+ IntToStr(i) +'= '+ IntToStr(Ord(suit)));
end;
end;
|
|
Myenum1= 1
Myenum2= 2
Myenum3= 10
Myenum4= 11
|