Difference between revisions of "Basic Pascal Tutorial/Chapter 3/CASE/ja"

From Free Pascal wiki
Jump to navigationJump to search
Line 3: Line 3:
 
3Cb - CASE (著者: Tao Yue, 状態: 原文のまま変更なし)
 
3Cb - CASE (著者: Tao Yue, 状態: 原文のまま変更なし)
  
Case は case 文を実行する。 case 文は各選択肢の順序表現を比較する。各選択肢は [[Const/ja|constant]]であったり、部分範囲、あるいは [[Comma/ja|commas]]で区切られたリストである。選択肢フィールドは[[Colon]]によってアクション・フィールドと分けられる。
+
Case は case 文を実行する。 case 文は各選択肢(selector)の順序表現を比較する。各選択肢は [[Const/ja|constant]]であったり、部分範囲、あるいは [[Comma/ja|commas]]で区切られたリストである。選択肢フィールドは[[Colon]]によってアクション・フィールドと分けられる。
  
<tt>b</tt> が <tt>1、 7、 2037、</tt> あるいは <tt>5</tt>のいずれかで1方向に分岐させ、それ以外の場合には他の処理をさせたとしよう。それには次のようになる。
+
<tt>b</tt> が <tt>1、 7、 2037、</tt> あるいは <tt>5</tt>のいずれかで1方向に分岐させ、それ以外の場合には他の処理をさせるとしよう。それは次のようになる。
 
<syntaxhighlight>
 
<syntaxhighlight>
 
if (b = 1) or (b = 7) or (b = 2037) or (b = 5) then
 
if (b = 1) or (b = 7) or (b = 2037) or (b = 5) then
Line 13: Line 13:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
But in this case, it would be simpler to list the numbers for which you want Statement1 to execute. You would do this with a <tt>case</tt> statement:
+
しかし、この場合は実行したい文に対応した数字をリストすることで簡単にできる。これは <tt>case</tt> 文で実行できる。
 
<syntaxhighlight>
 
<syntaxhighlight>
 
case b of
 
case b of
   1,7,2037,5: Statement1;
+
   1,7,2037,5: 文1;
   otherwise  Statement2
+
   otherwise  文2
 
end;
 
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The general form of the <tt>case</tt> statement is:
+
一般的な <tt>case</tt> 文の形式は次のようになる。
 
<syntaxhighlight>
 
<syntaxhighlight>
case selector of
+
case 選択肢 of
   List1:    Statement1;
+
   リスト1:    文1;
   List2:    Statement2;
+
   リスト2:    文2;
 
   ...
 
   ...
   Listn:    Statementn;
+
   リストn:    文n;
   otherwise Statement
+
   otherwise
 
end;
 
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The <tt>otherwise</tt> part is optional. When available, it differs from compiler to compiler. In many compilers, you use the word <tt>else</tt> instead of <tt>otherwise</tt>.
+
<tt>otherwise</tt> の部分はオプションである。利用できるとしてもコンパイラ次第で異なる。多くのコンパイラでは <tt>otherwise</tt> の代わりに <tt>else</tt> を使う。
  
selector is any variable of an ordinal data type. You may not use reals!
+
選択肢はなんらかの順序データ型である。実数は利用できない。
  
Note that the lists must consist of literal values. That is, you must use constants or hard-coded values -- you cannot use variables.
+
リストは文字値からなっていなくてはならないことに気をつけよう。つまり、定数か変更できない値を使わなくてはならない。言い換えれば変数は使えないのである。
  
 
{|style=color-backgroud="white" cellspacing="20"
 
{|style=color-backgroud="white" cellspacing="20"

Revision as of 18:13, 10 August 2015

Template:CASE/ja

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

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

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

if (b = 1) or (b = 7) or (b = 2037) or (b = 5) then
  文1
else
  文2;

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

case b of
  1,7,2037,5: 文1;
  otherwise   文2
end;

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

case 選択肢 of
  リスト1:    文1;
  リスト2:    文2;
  ...
  リストn:    文n;
  otherwise 文
end;

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

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

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

previous contents next