procedure TForm1.Button1Click(Sender: TObject);
var
iloop: Integer;
icount: Integer;
msg: AnsiString;
begin
// Ex.1:for
// 1づつカウントアップの例
icount := 0;
msg := '';
for iloop := 1 to 10 do
begin
Inc( icount );
msg := msg + IntToStr( iloop ) + ', ';
end;
msg := msg + #13#10 + IntToStr( icount ) + '回forループしました';
ShowMessage( msg );
// Ex.2:for
// 2づつカウントアップの例
// VBやC/C++のようにfor文で増分の変更ができない
// よってwhile ..do文で代用する
iloop := 1;
icount := 0;
msg := '';
while iloop <= 10 do
begin
Inc( icount );
msg := msg + IntToStr( iloop ) + ', ';
Inc( iloop, 2 );
end;
msg := msg + #13#10 + IntToStr( icount ) + '回for(while do)ループしました';
ShowMessage( msg );
// Ex.3:for
// カウントダウンの例
icount := 0;
msg := '';
for iloop := 10 downto 1 do
begin
Inc( icount );
msg := msg + IntToStr( iloop ) + ', ';
end;
msg := msg + #13#10 + IntToStr( icount ) + '回forループしました';
ShowMessage( msg );
// Ex.4:for
// カウントアップ中にループ脱出の例
icount := 0;
msg := '';
for iloop := 1 to 10 do
begin
if iloop = 5 then
begin
Break; // ループ脱出
end;
Inc( icount );
msg := msg + IntToStr( iloop ) + ', ';
end;
msg := msg + #13#10 + IntToStr( icount ) + '回forループしました';
ShowMessage( msg );
// Ex.5:for
// カウントアップ中にループ先頭に戻る例
icount := 0;
msg := '';
for iloop := 1 to 10 do
begin
if ( iloop mod 2 ) = 1 then
begin
Continue; // ループ先頭へ
end;
Inc( icount );
msg := msg + IntToStr( iloop ) + ', ';
end;
msg := msg + #13#10 + IntToStr( icount ) + '回forループしました';
ShowMessage( msg );
end;
|