summaryrefslogtreecommitdiff
path: root/fpcsrc/tests/test/tforin1.pp
blob: bb9a67bd0e904fe2e2f0897141346c623ff1d2e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
program tforin1;

// test for-in loop for the basic types: Enumeration, Range, Array, String and Set

{$mode objfpc}{$H+}
{$apptype console}
uses
  Classes, typinfo;

type
  TColor = (clRed, clGreen, clBlue);

procedure LoopTypes;
type
  IntRange = 0..9;
var
  c: TColor;
  i: integer;
begin
  // check loop in range
  for i in IntRange do
    writeln(i);

  // check loop in enum
  for c in TColor do
    writeln(GetEnumName(TypeInfo(c), ord(c)));
end;

procedure LoopString(s: string);
var
  c: char;
begin
  // check loop in string
  for c in s do
    write(c);
  WriteLn;
end;

procedure LoopArray(a: array of integer);
var
  i: integer;
begin
  // check loop in array
  for i in a do
    write(i);
  WriteLn;
end;

procedure LoopSet;
const
  s = [clRed, clBlue];
var
  c: TColor;
begin
  // check loop in set
  for c in s do
    writeln(GetEnumName(TypeInfo(c), ord(c)));
end;

var
  a: array[0..2] of integer;
begin
  LoopTypes;
  LoopString('test');
  a[0]:=0;
  a[1]:=1;
  a[2]:=2;
  LoopArray(a);
  LoopSet;
end.