Difference between revisions of "Basic Pascal Tutorial/Chapter 1/Assignment and Operations/ja"

From Free Pascal wiki
Jump to navigationJump to search
Line 50: Line 50:
 
<tt>some_real</tt>は<tt>375.0</tt>の値になる。
 
<tt>some_real</tt>は<tt>375.0</tt>の値になる。
  
あるデータ・タイプを別なデータ・タイプに変換することは型キャストと呼ばれる。現在の Pascal コンパイラは幾分異なる構文を持つ C のように明示的な型キャストをサポートしている。However, typecasting is usually used in low-level situations and in connection with object-oriented programming, and a beginning programming student will not need to use it. Here is information on typecasting from the GNU Pascal manual.
+
あるデータ・タイプを別なデータ・タイプに変換することは型キャストと呼ばれる。現在の Pascal コンパイラは幾分異なる構文を持つ C のように明示的な型キャストをサポートしている。しかしながら、型キャストはたいてい、低レベルの状況でオブジェクト指向プログラミングとの関係で使われる。だから、プログラミングに初心者の学生がそれを使う必要はない。ここには GNU Pascal マニュアルの型キャストの情報を示しておく。
  
In Pascal, the minus sign can be used to make a value negative. The plus sign can also be used to make a value positive, but is typically left out since values default to positive.
+
Pascal においてはマイナス符号は値をマイナスにするために利用できる。同様にプラスの符号は値をプラスにするために使える。しかし、デフォルトの値はプラスなので通常、プラス符号は省略される。
  
 
Do not attempt to use two operators side by side, like in:
 
Do not attempt to use two operators side by side, like in:

Revision as of 20:59, 19 July 2015

български (bg) Deutsch (de) English (en) español (es) français (fr) 日本語 (ja) 한국어 (ko) русский (ru) 中文(中国大陆)‎ (zh_CN)

1E - 代入と演算子 (著者: Tao Yue, 状態: 原文のまま)

いったん、変数を宣言したなら、そこに値を入れることができる。これは代入と呼ばれる。

値を変数に割り当てるためには、以下の構文を使う。

variable_name := expression;

代入演算子に単に等号を使う他の言語とは違い、 Pascal ではコロンとその後に等号を使うことに気をつけよう。これはほとんどのコンピュータ代数システムで表現されているものと似ている。

以下のように単一の値でもよいし、

some_real := 385.385837;

次のように算術式でもよい。

some_real := 37573.5 * 37593 + 385.8 / 367.1;

Pascal における算術的な操作には次のようなものがある。

Operator Operation Operands Result
+ Addition or unary positive real or integer real or integer
- Subtraction or unary negative real or integer real or integer
* Multiplication real or integer real or integer
/ Real division real or integer real
div Integer division integer integer
mod Modulus (remainder division) integer integer

divmodintegers(整数型)でのみ使える。/reals(実数型)とintegers(整数型)の両方で使えるが、得られるのは常にreal の答えである。他の操作はrealsintegersの両方で使える。integersrealsを共に使用した場合、結果は常にrealになるだろう。そうしないとデータの損失が生じてしまうからである。これが Pascal において割り算と整数割り算という2つの異なる割り算が用いられる理由である。7 / 2 = 3.5 (real)である。しかし、7 div 2 = 3となる (そして7 mod 2 = 1である。余りがあるからである)。

各変数には同じデータ型である値だけが代入される。従って、整数型に実数型を代入できない。しかしながら、あるデータ・タイプは高次のデータ・タイプに変換される。これは整数値を実数変数に代入するときに最もしばしば起きていることである。次のような変数宣言部があるとしよう。

var
  some_int : integer;
  some_real : real;

以下のようなステートメントが実行されると、

some_int := 375;
some_real := some_int;

some_real375.0の値になる。

あるデータ・タイプを別なデータ・タイプに変換することは型キャストと呼ばれる。現在の Pascal コンパイラは幾分異なる構文を持つ C のように明示的な型キャストをサポートしている。しかしながら、型キャストはたいてい、低レベルの状況でオブジェクト指向プログラミングとの関係で使われる。だから、プログラミングに初心者の学生がそれを使う必要はない。ここには GNU Pascal マニュアルの型キャストの情報を示しておく。

Pascal においてはマイナス符号は値をマイナスにするために利用できる。同様にプラスの符号は値をプラスにするために使える。しかし、デフォルトの値はプラスなので通常、プラス符号は省略される。

Do not attempt to use two operators side by side, like in:

some_real := 37.5 * -2;

This may make perfect sense to you, since you're trying to multiply by negative-2. However, Pascal will be confused — it won't know whether to multiply or subtract. You can avoid this by using parentheses to clarify:

some_real := 37.5 * (-2);

The computer follows an order of operations similar to the one that you follow when you do arithmetic. Multiplication and division (* / div mod) come before addition and subtraction (+ -), and parentheses always take precedence. So, for example, the value of: 3.5*(2+3) will be 17.5.

Pascal cannot perform standard arithmetic operations on Booleans. There is a special set of Boolean operations. Also, you should not perform arithmetic operations on characters.

previous contents next