(case ラベル), … ,(case ラベル):(実行文);
:
(case ラベル), … ,(case ラベル):(実行文);
end;
for (制御変数):=(初期値) downto (最終値) do (実行文);
前者は 初期値 ≦ 最終値 のとき、 後者は 初期値 ≧ 最終値 のときで、 この不等号が成り立たない場合には実行文は一度も実行されない。
実行文を最低1回実行する点が while ループと異なる。
program sample4a; { 角谷予想 }
{ [偶数なら2で割る、奇数なら3倍して1を足す]
という操作を自然数に繰り返すと必ず1になるという予想がある。}
var
n,max:integer;
over:boolean;
begin
max:=(maxint-1) div 3;
over:=false;
repeat
write('2 以上 ',max:1,' 以下の自然数を入力してください : ');
readln(n);
until (n>=2) and (n<=max);
write(n:1);
while n<>1 do
begin
if (n>max) or (n<0) then begin over:=true; exit end;
if (n mod 2)=0
then n:=n div 2
else n:=3*n+1;
write(' → ',n:1)
end;
writeln;
if over=true then writeln('overflow occurs !!');
end.
program sample4b;
var
ok:boolean;
sentaku,stepsuu:integer;
shokichi,haba:real;
loguni:real;
procedure table(function f(x:real):real; a,b:real; c:integer);
var i:integer;
begin
for i:=1 to c do
begin
writeln('x = ',a:10:5,', f(x) = ',f(a):10:5);
a:=a+b
end
end;
function nibeki(x:real):real;
begin nibeki:=exp(loguni*x) end;
function heihoukon(x:real):real;
begin heihoukon:=sqrt(x) end;
function sanjoukon(x:real):real;
begin sanjoukon:=exp(ln(x)/3) end;
{ 関数や手続きも他の関数や手続きの引数にできる。テキスト pp.86-88 参照 }
begin
loguni:=ln(2);
writeln('数表を作成します。');
writeln('1 : 2のべき, 2 : 平方根, 3 : 3乗根');
writeln;
repeat
write('1, 2 または 3 を入力してください : ');
readln(sentaku);
if (sentaku>=1) and (sentaku<=3)
then ok:=true
else begin ok:=false; writeln('再入力') end;
until ok=true;
write('数表の初期値を入力してください : ');
readln(shokichi);
write('数表の刻み幅を入力してください : ');
readln(haba);
write('数表のステップ数を入力してください : ');
readln(stepsuu);
writeln;
case sentaku of
1: table(nibeki,shokichi,haba,stepsuu);
2: table(heihoukon,shokichi,haba,stepsuu);
3: table(sanjoukon,shokichi,haba,stepsuu)
end;
end.