تستعمل الصيغتين while و For لتكرر أمر معين حتى يتحقق شرط أو شروط معينة ، وهذه أمثلة عن استعمالاتهما :
unit Unit1; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls, FMX.Layouts, FMX.Memo, FMX.Controls.Presentation, FMX.ScrollBox; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Button5: TButton; Memo1: TMemo; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button5Click(Sender: TObject); private { Déclarations privées } public { Déclarations publiques } procedure Show(const msg: string); end; var Form1: TForm1; implementation {$R *.fmx} { TForm1 } uses System.Diagnostics; procedure TForm1.Button1Click(Sender: TObject); var I: Integer; begin for I := 1 to 20 do Show('Number ' + IntToStr(I)); end; procedure TForm1.Button2Click(Sender: TObject); var I: Integer; begin I := 1; while I <= 20 do begin Show('Number ' + IntToStr(I)); Inc(I, 2) end; end; procedure TForm1.Button3Click(Sender: TObject); var I: Integer; begin Randomize; I := 1; while I < 500 do begin Show('Random Number: ' + IntToStr(I)); I := I + Random(100); end; end; procedure TForm1.Button4Click(Sender: TObject); var s: string; ch: Char; begin s := 'Hello world'; for ch in s do Show(ch); end; procedure TForm1.Button5Click(Sender: TObject); var s: string; I: Integer; Total: Integer; SW: TStopWatch; ch: Char; begin // create a thirty million char string s := ''; for I := 1 to 30000000 do begin // take the first char of the number s := s + IntToStr(I).Chars[1]; end; SW := TStopWatch.StartNew; Total := 0; for I := Low(s) to High(s) do begin Total := Total + Ord(s[I]); end; SW.Stop; Show('for ' + Total.ToString + ' [' + SW.ElapsedMilliseconds.ToString + '] '); SW := TStopWatch.StartNew; Total := 0; for ch in s do begin Total := Total + Ord(ch); end; SW.Stop; Show('for ' + Total.ToString + ' [' + SW.ElapsedMilliseconds.ToString + '] '); end; procedure TForm1.Show(const msg: string); begin Memo1.Lines.Add(msg); end; end.//********************************************