プリンキピア

第7章高水準プログラミング言語

Named Constants名前付き定数
A variable may hold different values within the data type restrictions of the language at various times during program execution. There are times when we would like a value to be assigned to a specific memory location and not allow it to be changed thereafter. Most modern HLLs and associated DLLs (dynamic link libraries) provide a construct for this purpose called a named constant. It is also a best practice in most language cultures to use capitals with underscores separating multiple words. The following statement in C++ creates a named constant:
英語のヒント
変数は、プログラム実行中のさまざまな時点で、言語のデータ型の制約内で異なる値を持てる。特定のメモリ位置に値を割り当て、その後は変更できなくしたい場合もある。現代のHLLの大半と、それに関連するDLL(動的リンクライブラリー)は、このために名前付き定数という構造を提供する。また、多くの言語の慣習では、すべて大文字にし、複数の語をアンダースコアで区切るのが推奨される。次のC++の文は、名前付き定数を作る。
const int PAY_RATE = 15;
The word const is a keyword, int is a C++ integer data type followed by the name of the constant, equals (=) is the assignment operator followed by the rvalue, and the statement ends with a terminator. It is a best practice in programming to use named constants whenever indicated and possible. In this example, if an employee pay rate was to change, it would only have to be changed in one spot in the program rather than having to locate its usages in many lines of code.
英語のヒント
constはキーワードであり、intはC++の整数データ型で、その後ろに定数名が続く。等号(=)は代入演算子で、その後ろに右辺値が続き、文は終端記号で終わる。プログラミングでは、適切で可能な場合はいつでも、名前付き定数を使うことが望ましい。この例で従業員の給与単価が変わることになっても、コードの多くの行から使用箇所を探さずに、プログラムの一か所だけを変更すれば済む。
Operators演算子
Much like in algebra, an operator in HLLs performs various types of operations (processes) on values.
代数とよく似て、HLLの演算子は、値に対してさまざまな種類の演算、つまり処理を行う。
Different HLLs may support operators differently. Some examples of the types of operations by which values may be manipulated are arithmetic operators (mathematical), relational operators (comparison), logical operators, and string operators (sequences of characters). They perform these operations within expressions of the language. To review, an expression is a construct in a programming language that evaluates two values. Operators may act upon different numbers of operands, usually one (a unary operator), two (a binary operator), or three (a ternary operator). Operators perform under certain rules that dictate the order of operations, or precedence. Although many HLLs use the same operators, when learning a new language, it is necessary to research its operators to identify the very few exceptions.
英語のヒント
演算子への対応は、HLLによって異なる場合がある。値を操作する演算の種類には、算術演算子(数学的なもの)、関係演算子(比較)、論理演算子、文字列演算子(文字の並び)などがある。これらは、言語の式の中で演算を行う。復習すると、式とは、二つの値を評価する、プログラミング言語の構造である。演算子が扱うオペランドの数は異なり、通常は、一つなら単項演算子、二つなら二項演算子、三つなら三項演算子という。演算子は、演算の順序、つまり優先順位を定める規則に従う。多くのHLLは同じ演算子を使うが、新しい言語を学ぶときは、その言語の演算子を調べ、少数の例外を見つける必要がある。
Arithmetic Operators
算術演算子
The arithmetic operators perform the four familiar mathematical operations on their operands: addition (+), subtraction (-), multiplication (*), and division (/). There is one more unfamiliar operation known as modulo operator (%), which evaluates to the remainder left after division. Some languages also employ an exponentiation operator (**), the operator that raises the value of one operand to the power of the second operand. The increment operator (++) and decrement operator (--) raise or lower the value in a variable by one, respectively, and are used in the vast majority of HLLs. These Java arithmetic operators are outlined in Table 7.3.
英語のヒント
算術演算子は、オペランドに対して、なじみのある四つの数学的な演算、加算(+)、減算(-)、乗算(*)、除算(/)を行う。もう一つ、あまりなじみのない剰余演算子(%)があり、割り算の余りを求める。累乗演算子(**)を使う言語もあり、一方のオペランドを、もう一方を指数とするべき乗にする。増分演算子(++)と減分演算子(--)は、変数の値をそれぞれ1増やす、または1減らすもので、HLLの大半で使われる。Javaのこれらの算術演算子を、表7.3にまとめる。
Operator
演算子
Name
名称
Example Expression
式の例
Meaning
意味
*
Multiplication
乗算
a * b
a times b
aとbの積
/
Division
除算
a / b
a divided by b
aをbで割る
%
Remainder (modulus)
余り(剰余)
a % b
remainder after dividing a by b
aをbで割った余り
+
Addition
加算
a + b
a plus b
aとbの和
-
Subtraction
減算
a - b
a minus b
aからbを引く
Table 7.3 The Java Arithmetic Operators
表7.3 Javaの算術演算子
Relational Operators
関係演算子
The relational operators compare their operands, and expressions using them evaluate to the Boolean values true or false. Table 7.4 lists the relational operator symbols that are used in the vast majority of HLLs.
英語のヒント
関係演算子は、オペランド同士を比較し、それを使う式の評価結果は、真偽値のtrueまたはfalseになる。表7.4に、HLLの大半で使う関係演算子の記号を示す。
Operator
演算子
Name
名称
Example Expression
式の例
Result
結果
<
Less than
より小さい
1 < 2
True
True(真)
>
Greater than
より大きい
1 > 2
False
False(偽)
<=
Less than or equal to
以下
1 <= 2
True
True(真)
>=
Greater than or equal to
以上
1 >= 2
False
False(偽)
Table 7.4 The C# Relational Operators
表7.4 C#の関係演算子
Operator
演算子
Name
名称
Example Expression
式の例
Result
結果
==
Equal to
等しい
1 == 2
False
False(偽)
!=
Not equal to
等しくない
1 != 2
True
True(真)
Table 7.4 The C# Relational Operators
表7.4 C#の関係演算子
Logical Operators
論理演算子
The logical operators are used to connect two or more expressions. They evaluate the entire expression to the Boolean values true or false. Table 7.5 lists the logical operator symbols that are used in the vast majority of HLLs.
英語のヒント
論理演算子は、二つ以上の式を結び付けるために使う。式全体を評価し、真偽値のtrueまたはfalseを求める。表7.5に、HLLの大半で使う論理演算子の記号を示す。
Operator
演算子
Name
名称
Example Expression
式の例
Result
結果
&&
Logical AND
論理積(AND)
(1 < 2) && (2 > 1)
True
True(真)
||
Logical OR
論理和(OR)
(1 < 2) || (2 > 1)
True
True(真)
!
Logical NOT
論理否定(NOT)
!(1 < 2)
False
False(偽)
Table 7.5 The JavaScript Logical Operators
表7.5 JavaScriptの論理演算子
Expressions using logical operators are evaluated based on a truth table, a chart that shows what the resulting value would be given each combination of operands. Table 7.6 shows the truth tables for the three logical operations we have studied.
英語のヒント
論理演算子を使う式は、真理値表に基づいて評価する。真理値表は、オペランドの各組み合わせを与えたとき、結果がどの値になるかを示す表である。表7.6は、学んだ三つの論理演算の真理値表を示す。
A
B
A && B
A || B
!A
True
True(真)
True
True(真)
True
True(真)
True
True(真)
False
False(偽)
True
True(真)
False
False(偽)
False
False(偽)
True
True(真)
False
False(偽)
False
False(偽)
True
True(真)
False
False(偽)
True
True(真)
True
True(真)
False
False(偽)
False
False(偽)
False
False(偽)
False
False(偽)
True
True(真)
Table 7.6 Truth Table for Logical AND, OR, and NOT Operators
表7.6 論理積、論理和、論理否定の演算子の真理値表
Combined Assignment
複合代入
The assignment operator may be combined with other operators, usually mathematical, as a shortcut notation. This is called combined assignment. In this construct, the operation is carried out first, followed by the assignment. Table 7.7 shows the most common combined assignment expressions.
英語のヒント
代入演算子は、省略記法として、ほかの演算子、通常は算術演算子と組み合わせられる。これを複合代入という。この構文では、まず演算を行い、その後に代入する。表7.7は、最もよく使う複合代入式を示す。
Operator
演算子
Example
例
Equivalent to
等価な式
+=
x += 7
x = x + 7
-=
y -= 4
y = y - 4
*=
z *= 2
z = z * 2
False
False(偽)
a /= b
a = a / b
False
False(偽)
c %= 9
c = c % 9
Table 7.7 Combined Assignment Operators
表7.7 複合代入演算子