forループの例

1716 ワード



unit Unit1;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls;



type

  TForm1 = class(TForm)

    Button1: TButton;

    Button2: TButton;

    Button3: TButton;

    Button4: TButton;

    procedure Button1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

    procedure Button3Click(Sender: TObject);

    procedure Button4Click(Sender: TObject);

    procedure FormCreate(Sender: TObject);

    procedure FormDestroy(Sender: TObject);

  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



var

  List: TStringList;



procedure TForm1.FormCreate(Sender: TObject);

begin

  List := TStringList.Create;

  List.Add('aaa');

  List.Add('bbb');

  List.Add('ccc');

  List.Add('ddd');

end;





// for  

procedure TForm1.Button1Click(Sender: TObject);

var

  i: Integer;

  s: string;

begin

  for i := 0 to List.Count - 1 do

  begin

    s := List[i];

    ShowMessage(s);

  end;

end;





// 

procedure TForm1.Button2Click(Sender: TObject);

var

  i: Integer;

  s: string;

begin

  for i := 0 to Pred(List.Count) do

  begin

    s := List[i];

    ShowMessage(s);

  end;

end;





//  for in  

procedure TForm1.Button3Click(Sender: TObject);

var

  s: string;

begin

  for s in List do

  begin

    ShowMessage(s);

  end;

end;





// 

procedure TForm1.Button4Click(Sender: TObject);

var

  i: Integer;

  s: string;

begin

  for i := List.Count - 1 downto 0 do

  begin

    s := List[i];

    ShowMessage(s);

  end;

end;





procedure TForm1.FormDestroy(Sender: TObject);

begin

  List.Free;

end;



end.