For循环

For循环


 


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.


 

DoubleCat QQ:28088191

你可能感兴趣的:(for循环)