Difference between revisions of "Basic Pascal Tutorial/Chapter 1/Assignment and Operations/ja"
Line 35: | Line 35: | ||
|mod ||Modulus (remainder division) ||integer ||integer | |mod ||Modulus (remainder division) ||integer ||integer | ||
|} | |} | ||
− | '''<tt>div</tt>''' と '''<tt>mod</tt>''' は ''integers'' | + | '''<tt>div</tt>''' と '''<tt>mod</tt>''' は ''integers''(整数型)でのみ使える。'''<tt>/</tt>''' は''reals''(実数型)と''integers''(整数型)の両方で使えるが、得られるのは常に''real'' の答えである。他の操作は''reals''と''integers''の両方で使える。''integers''と''reals''を共に使用した場合、結果は常に''real''になるだろう。そうしないとデータの損失が生じてしまうからである。これが Pascal において割り算と整数割り算という2つの異なる割り算が用いられる理由である。<tt>7 / 2 = 3.5</tt> (real)である。しかし、<tt>7 div 2 = 3</tt>となる (そして<tt>7 mod 2 = 1</tt>である。余りがあるからである)。 |
− | + | 各変数には同じデータ型である値だけが代入される。従って、整数型に実数型を代入できない。しかしながら、あるデータ・タイプは高次のデータ・タイプに変換される。これは整数値を実数変数に代入するときに最もしばしば起きていることである。次のような変数宣言部があるとしよう。 | |
<syntaxhighlight> | <syntaxhighlight> | ||
var | var | ||
Line 43: | Line 43: | ||
some_real : real; | some_real : real; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | 以下のようなステートメントが実行されると、 | |
<syntaxhighlight> | <syntaxhighlight> | ||
some_int := 375; | some_int := 375; | ||
some_real := some_int; | some_real := some_int; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | <tt>some_real</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. | |
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. | 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. |
Revision as of 12:18, 13 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 |
div と mod は integers(整数型)でのみ使える。/ はreals(実数型)とintegers(整数型)の両方で使えるが、得られるのは常にreal の答えである。他の操作はrealsとintegersの両方で使える。integersとrealsを共に使用した場合、結果は常に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_realは375.0の値になる。
あるデータ・タイプを別なデータ・タイプに変換することは型キャストと呼ばれる。現在の 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.
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.
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 |