Sign in Join
Wiki copytemporary

Revision 1 of 1, by WikiArchive on 11-4-2026. The page as it is now.

Basic Pascal Tutorial/Chapter 3/CASE/ja

3Cb - CASE (著者: Tao Yue, 状態: 原文のまま変更なし)

Case は case 文を実行する。 case 文は各選択肢(selector)の順序表現を比較する。各選択肢は constantであったり、部分範囲、あるいは commasで区切られたリストである。選択肢フィールドはColonによってアクション・フィールドと分けられる。

b が 1、 7、 2037、 あるいは 5のいずれかで1方向に分岐させ、それ以外の場合には他の処理をさせるとしよう。それは次のようになる。

pascal
if (b = 1) or (b = 7) or (b = 2037) or (b = 5) then
  æ–‡1
else
  æ–‡2;

しかし、この場合は実行したい文に対応した数字をリストすることで簡単にできる。これは case 文で実行できる。

pascal
case b of
  1,7,2037,5: æ–‡1;
  otherwise   æ–‡2
end;

一般的な case 文の形式は次のようになる。

pascal
case �択肢 of
  リスト1:    文1;
  リスト2:    文2;
  ...
  リストn:    文n;
  otherwise æ–‡
end;

otherwise の部分はオプションである。利用できるとしてもコンパイラ次第で異なる。多くのコンパイラでは otherwise の代わりに else を使う。

選択肢はなんらかの順序データ型である。実数は利用できない。

リストは文字値からなっていなくてはならないことに気をつけよう。つまり、定数か変更できない値を使わなくてはならない。言い換えれば変数は使えないのである。

previous contents next

From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 3/CASE/ja", as it stood on 2022-08-20 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.