1. 程式人生 > 其它 >Delphi 系統[9]關鍵字和保留字 for、to、downto、do、while、repeat、until

Delphi 系統[9]關鍵字和保留字 for、to、downto、do、while、repeat、until

Delphi 系統[9]關鍵字和保留字 for、to、downto、do、while、repeat、until

1、定義:

  • for..to(或downto)do組合使用,構成for迴圈語句。
  • while..do組合,構成while迴圈語句。
  • repeat..until組合,構成repeat迴圈語句。
  • for還可以與in組合,構成for迴圈語句,詳見in
  • do還可以與with組合構成預設物件語句,詳見with
  • do還可以與try..except..on組合使用,構成異常處理語句,詳見except。

2、示例:

{ for 迴圈語句,迴圈變數遞增 to } 
procedure TForm1.Button1Click(Sender: TObject); 
var 
  I, iSum: Integer; 
begin 
  iSum := 0; 
  for I := 1 to 100 do 
    iSum := iSum + I; 
  Caption := IntToStr(iSum);  { 結果: 5050 } 
end; 
 
{ for 迴圈語句,迴圈變數遞減 downto } 
procedure TForm1.Button1Click(Sender: TObject); 
var 
  I, iSum: Integer; 
begin 
  iSum := 0; 
  for I := 100 downto 1 do 
    iSum := iSum + I; 
  Caption := IntToStr(iSum);  { 結果: 5050 } 
end; 
 

{ while 語句 } 
procedure TForm1.Button1Click(Sender: TObject); 
var 
  I, iSum: Integer; 
begin 
  iSum := 0; 
  I := 0; 
  while I <= 100 do begin 
    iSum := iSum + I; 
    Inc(I); 
  end; 
  Caption := IntToStr(iSum);  { 結果:5050 } 
end; 
 

{ repeat 語句 } 
procedure TForm1.Button1Click(Sender: TObject); 
var 
  I, iSum: Integer; 
begin 
  iSum := 0; 
  I := 0; 
  repeat 
    iSum := iSum + I; 
    Inc(I); 
  until I > 100; 
  Caption := IntToStr(iSum);  { 結果: 5050 } 
end; 

  

建立時間:2021.08.11  更新時間:

部落格園 滔Roy https://www.cnblogs.com/guorongtao 希望內容對你所有幫助,謝謝!