Difference between revisions of "Basic Pascal Tutorial/Chapter 1/Assignment and Operations/ja"
Line 11: | Line 11: | ||
代入演算子に単に等号を使う他の言語とは違い、 Pascal ではコロンとその後に等号を使うことに気をつけよう。これはほとんどのコンピュータ代数システムで表現されているものと似ている。 | 代入演算子に単に等号を使う他の言語とは違い、 Pascal ではコロンとその後に等号を使うことに気をつけよう。これはほとんどのコンピュータ代数システムで表現されているものと似ている。 | ||
− | + | 以下のように単一の値でもよいし、 | |
<syntaxhighlight> | <syntaxhighlight> | ||
some_real := 385.385837; | some_real := 385.385837; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | 次のように算術式でもよい。 | |
<syntaxhighlight> | <syntaxhighlight> | ||
some_real := 37573.5 * 37593 + 385.8 / 367.1; | some_real := 37573.5 * 37593 + 385.8 / 367.1; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | Pascal における算術的な操作には次のようなものがある。 | |
{| style="background-color:#f5f5f5" cellspacing=5 | {| style="background-color:#f5f5f5" cellspacing=5 | ||
!Operator !!Operation !!Operands !!Result | !Operator !!Operation !!Operands !!Result | ||
Line 35: | Line 35: | ||
|mod ||Modulus (remainder division) ||integer ||integer | |mod ||Modulus (remainder division) ||integer ||integer | ||
|} | |} | ||
− | '''<tt>div</tt>''' | + | '''<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>である。余りがあるからである)。 |
Each variable can only be assigned a value that is of the same data type. Thus, you cannot assign a real value to an integer variable. However, certain data types will convert to a higher data type. This is most often done when assigning integer values to real variables. Suppose you had this variable declaration section: | Each variable can only be assigned a value that is of the same data type. Thus, you cannot assign a real value to an integer variable. However, certain data types will convert to a higher data type. This is most often done when assigning integer values to real variables. Suppose you had this variable declaration section: |
Revision as of 12:06, 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である。余りがあるからである)。
Each variable can only be assigned a value that is of the same data type. Thus, you cannot assign a real value to an integer variable. However, certain data types will convert to a higher data type. This is most often done when assigning integer values to real variables. Suppose you had this variable declaration section:
var
some_int : integer;
some_real : real;
When the following block of statements executes,
some_int := 375;
some_real := some_int;
some_real will have a value of 375.0.
Changing one data type to another is referred to as typecasting. Modern Pascal compilers support explicit typecasting in the manner of C, with a slightly different syntax. 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 |