Basic Pascal Tutorial/Chapter 3/CASE/ja: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Basic Pascal Tutorial/Chapter 3/CASE/ja
| Old | New | Line | |
|---|---|---|---|
| 1 | + | 3Cb - CASE (著者: Tao Yue, 状態: 原文のまま変更なし) | |
| 2 | + | ||
| 3 | + | Case は case 文を実行する。 case 文は各選択肢(selector)の順序表現を比較する。各選択肢は [[Const/ja|constantであったり]]、部分範囲、あるいは [[Comma/ja|commasで]]区切られたリストである。選択肢フィールドは[[Colon|Colonによってアクション]]・フィールドと分けられる。 | |
| 4 | + | ||
| 5 | + | `b` が `1、 7、 2037、` あるいは `5`のいずれかで1方向に分岐させ、それ以外の場合には他の処理をさせるとしよう。それは次のようになる。 | |
| 6 | + | ||
| 7 | + | ``` pascal | |
| 8 | + | if (b = 1) or (b = 7) or (b = 2037) or (b = 5) then | |
| 9 | + | 文1 | |
| 10 | + | else | |
| 11 | + | 文2; | |
| 12 | + | ``` | |
| 13 | + | ||
| 14 | + | しかし、この場合は実行したい文に対応した数字をリストすることで簡単にできる。これは `case` 文で実行できる。 | |
| 15 | + | ||
| 16 | + | ``` pascal | |
| 17 | + | case b of | |
| 18 | + | 1,7,2037,5: 文1; | |
| 19 | + | otherwise 文2 | |
| 20 | + | end; | |
| 21 | + | ``` | |
| 22 | + | ||
| 23 | + | 一般的な `case` 文の形式は次のようになる。 | |
| 24 | + | ||
| 25 | + | ``` pascal | |
| 26 | + | case 選択肢 of | |
| 27 | + | リスト1: 文1; | |
| 28 | + | リスト2: 文2; | |
| 29 | + | ... | |
| 30 | + | リストn: 文n; | |
| 31 | + | otherwise 文 | |
| 32 | + | end; | |
| 33 | + | ``` | |
| 34 | + | ||
| 35 | + | `otherwise` の部分はオプションである。利用できるとしてもコンパイラ次第で異なる。多くのコンパイラでは `otherwise` の代わりに `else` を使う。 | |
| 36 | + | ||
| 37 | + | 選択肢はなんらかの順序データ型である。実数は利用できない。 | |
| 38 | + | ||
| 39 | + | リストは文字値からなっていなくてはならないことに気をつけよう。つまり、定数か変更できない値を使わなくてはならない。言い換えれば変数は使えないのである。 | |
| 40 | + | ||
| 41 | + | | | | | | |
| 42 | + | |----|----|----| | |
| 43 | + | | [[Basic Pascal Tutorial/Chapter 3/IF/ja|previous]] | [[Basic Pascal Tutorial/Contents/ja|contents]] | [[Basic Pascal Tutorial/Chapter 3/FOR..DO/ja|next]] | | |
| 44 | + | ||
| 45 | + | --- | |
| 46 | + | *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](/wiki-contact).* |
