プリンキピア

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

Variables変数

We learned in 7.1 Programming Language Foundations that a variable is a container that is used in an HLL to hold a value. In computer science we have many instances of this type of construct, which we call a name-value pair, a construct-like variable that is named and can hold values. The types of values that they may hold consist of the legal data types of the language.
英語のヒント
「7.1 プログラミング言語の基礎」で、変数は、HLLで値を保持するための入れ物であると学んだ。コンピューターサイエンスには、この種類の構造の例が数多くある。これを名前と値の組と呼ぶ。名前が付き、値を保持できる、変数のような構造である。保持できる値の種類は、その言語の規則で認められたデータ型からなる。
Identifiers識別子
A variable name is called an identifier. Different HLLs have different rules about legal identifier syntax. For example, in C# rules are as follows:
英語のヒント
変数の名前を、識別子という。どのような識別子の構文を認めるかは、HLLによって異なる。たとえば、C#の規則は次のとおりである。
• An identifier cannot be a keyword, which is a word reserved by the language and that has a special meaning.
英語のヒント
• 識別子をキーワードにすることはできない。キーワードは、言語が予約し、特別な意味を持たせた語である。
• A letter, @symbol, or an underscore must start an identifier while the remaining portion may be digits, the underscore symbol, and/or letters (different from this, an identifier in PHP starts with a dollar sign ($)).
英語のヒント
• 識別子は、文字、@記号、またはアンダースコアで始めなければならない。残りの部分には、数字、アンダースコア、文字を使える。これとは異なり、PHPの識別子はドル記号($)で始まる。
• Identifiers are case-sensitive, where uppercase and lowercase letters are treated as distinct. Therefore, the C# identifier myAge is a different entity than myAGE (Fortran, BASIC, and Pascal are not case-sensitive).
英語のヒント
• 識別子は、大文字と小文字を区別し、別のものとして扱う。そのため、C#のmyAgeとmyAGEは、異なる識別子である。Fortran、BASIC、Pascalは、大文字と小文字を区別しない。
GLOBAL ISSUES IN TECHNOLOGY技術をめぐる世界的課題
Learning About Programming: A Language Barrier for Non-English Speakers Learning HLLs?プログラミングを学ぶ:英語を話さない人のHLL学習には言語の壁があるか
Have you ever struggled to understand something because it was explained in a language that you do not speak? That is the challenge that many non-English speakers face when learning HLLs. Most HLLs use English keywords that make sense to the compiler but not necessarily to someone unfamiliar with the language. Since programming has become a worldwide endeavor, English keywords can be a stumbling block for non-English speakers learning HLLs. Fortunately, there is a bright side! While keywords are in English, they comprise a relatively small set of words in a program. The real power of programming lies in its ability to work with data and instructions in any language, which is made possible via Unicode. Unicode can represent most international character sets, allowing programmers to use characters from almost any language alongside the English keywords.
英語のヒント
自分が話せない言語で説明されていたため、理解するのに苦労したことはあるだろうか。これが、HLLを学ぶ多くの非英語話者が直面する課題である。HLLの大半は、コンパイラーには意味が通じても、英語に不慣れな人には必ずしも通じない、英語のキーワードを使う。プログラミングが世界的な活動になったため、英語のキーワードは、HLLを学ぶ非英語話者の障害になりうる。幸い、明るい側面もある。キーワードは英語だが、プログラム中では比較的少数の語からなる。プログラミングの本当の力は、どの言語のデータや命令でも扱えることにあり、これはUnicodeによって可能になる。Unicodeは世界の文字集合の大半を表せるため、プログラマーは英語のキーワードと並べて、ほぼどの言語の文字も使える。
But what about the future? As technology evolves, will programming languages find ways to become even more natural language-independent? Perhaps future HLLs will offer interchangeable keywords or entirely new approaches that do not rely on any given language.
英語のヒント
では、未来はどうだろうか。技術が進むにつれ、プログラミング言語は、自然言語からさらに独立する方法を見つけるだろうか。将来のHLLは、交換可能なキーワードや、特定の言語に依存しない、まったく新しい方法を提供するかもしれない。
Variable Declarations変数宣言
A variable must be made known to a compiler or an interpreter before it may be used by a computer program. This process is variable declaration and/or definition. In strongly typed languages, a variable declaration consists of a statement which specifies the variable name and data type. Weakly-typed languages omit the data type when values are assigned to the variable, which may be different types at different times.
英語のヒント
変数は、プログラムが使う前に、コンパイラーやインタープリターへ知らせなければならない。この処理を、変数の宣言や定義という。強い型付けの言語では、変数宣言は、変数名とデータ型を指定する文からなる。弱い型付けの言語では、変数へ値を代入するときにデータ型を省き、その値の型は時点によって変わる場合がある。
Variable definitions in various languages are as follows:
各言語の変数定義は、次のとおりである。
Java: int myAge;
Java:int myAge;
JavaScript: var myAge;
JavaScript:var myAge;
PHP: $myAge = 21;
PHP:$myAge = 21;
Assigning its first value to a variable is known as initialization, which may be done at any time after declaration, such as in the following Java snippet:
英語のヒント
変数へ最初の値を代入することを初期化という。次のJavaの短いコードのように、宣言後であれば、いつ行ってもよい。
int myAge;
myAge = 21;
It is a best practice to always initialize variables when they are declared. This is known as declaration and initialization. This keeps the value that is stored from being undefined at any time, which can have grave consequences in code in various situations. For example, in the C programming language, failing to initialize a pointer to an array of characters in a program and copying a string of characters to the (uninitialized) memory location referred to by that pointer later in the program will crash the program. Here is another example in Java:
英語のヒント
変数は、宣言するときに必ず初期化するのが望ましい方法である。これを、宣言と初期化という。そうすれば、保存された値が未定義の状態になることを防げる。未定義の値は、さまざまな状況で、コードに重大な結果をもたらす可能性がある。たとえばC言語では、プログラム内で文字配列へのポインターを初期化しないまま、後で、そのポインターが指す未初期化のメモリ位置へ文字列をコピーすると、プログラムがクラッシュする。次は、Javaの別の例である。
int myAge = 21;
Assignment代入
A literal is a value of one of the legal data types of an HLL that can be written directly into the code. For example, in JavaScript, one of the data types is numeric, which may be represented by either the literal whole number 2 or by the floating-point number 2.0. In C++, a literal of the type char may be written as the single quoted sequence a.
英語のヒント
リテラルは、HLLの規則で認められたデータ型の値を、コードへ直接書き込んだものである。たとえば、JavaScriptには数値型があり、整数リテラルの2でも、浮動小数点数の2.0でも表せる。C++では、char型のリテラルを、aという並びを単一引用符で囲んで書ける。
Storing a value in a variable is carried out by creating an assignment statement: The value assigned may be a literal, or it may be the value that has been placed in another variable or the result of an expression. The value in a variable may also be replaced by using assignment. Therefore, variables may hold different values at different times.
英語のヒント
変数へ値を保存するには、代入文を作る。代入する値は、リテラルでも、別の変数に入っている値でも、式の結果でもよい。代入によって、変数内の値を置き換えることもできる。そのため、変数は、時点によって異なる値を持てる。
In a PHP expression that makes up an assignment as shown, the variable is located at the left. Notice that the identifier starts with the dollar sign ($), complying with the identifier rules of PHP. The equals sign (=) is known as the assignment operator, as in most languages with C-like syntax (C/C++, Java, C#, Python, JavaScript, PHP).
英語のヒント
次に示すPHPの代入式では、変数を左側へ置く。識別子が、PHPの規則に従ってドル記号($)で始まることに注目しよう。Cに似た構文の言語(C/C++、Java、C#、Python、JavaScript、PHP)の大半と同じく、等号(=)を代入演算子として使う。
$myAge = 21;
In programming languages, we refer to the left hand of a variable assignment statement (the variable) as the lvalue. The right-hand value (the literal) is referred to as the rvalue. The assignment operator is a binary operator, meaning it is surrounded by two operands. The operand is the lvalue or the rvalue on either side of the operator. The rvalue of a variable assignment statement may be the value of another variable as shown here or the result of an expression. An example of this in Java is as follows:
英語のヒント
プログラミング言語では、変数の代入文の左側、つまり変数を、左辺値(lvalue)と呼ぶ。右側の値、つまりリテラルを、右辺値(rvalue)と呼ぶ。代入演算子は、二つのオペランドに挟まれる二項演算子である。オペランドは、演算子の左右にある左辺値または右辺値を指す。代入文の右辺値は、ここに示すように別の変数の値でも、式の結果でもよい。Javaの例を次に示す。
myAge = yourAge;
Let us examine the concept a little more deeply. Variables may be named memory locations. We give a variable a name so that it is easy for humans to deal with it. It is a best practice to use names that are indicative of both the purpose of the variable (what it will be used for) and the data type that it will hold. So the variable myAge in the previous example meets both characteristics. One HLL best practice is to use an agreed upon convention for variable names. An example is camelCase, a naming convention that eliminates spaces and punctuation in favor of capitalization of specific words; in this case, the first letter of the first word is lowercase and if the name has multiple words, the later words start with a capital letter (e.g., firstName and lastName). Other conventions exist such as snake case (e.g., first_name, last_name), kebab case (e.g., first-name, last-name), and Pascal case (e.g., FirstName, LastName).
英語のヒント
この考えを、もう少し詳しく調べよう。変数は、名前を付けたメモリ位置とみなせる。人間が扱いやすいように名前を与える。変数の用途、つまり何に使うかと、保持するデータ型の両方を示す名前にするのが望ましい。前の例のmyAgeは、両方の特徴を満たす。HLLで推奨される方法の一つは、変数名に合意された規約を使うことである。一例はcamelCaseで、空白や句読点をなくす代わりに、特定の語を大文字で始める命名規約である。最初の語の最初の文字は小文字にし、名前が複数の語を含む場合、後の語は大文字で始める。たとえばfirstName、lastNameである。ほかにも、スネークケース(first_name、last_name)、ケバブケース(first-name、last-name)、パスカルケース(FirstName、LastName)などがある。
When a program is compiled, the compiler allocates a memory location to hold variables’ values and reserves the amount of memory necessary to hold such value based on the data type of the variable. The addresses of the memory locations that the compiler assigns to variables are relocatable, meaning that the linker may change these addresses when creating the executable version of the program, and the program loader will also change them when running the program to match actual memory addresses in the machine memory. Figure 7.11 illustrates what this looks like.
英語のヒント
プログラムをコンパイルすると、コンパイラーは、変数の値を保持するメモリ位置を割り当て、変数のデータ型に基づいて、必要なメモリ量を確保する。コンパイラーが変数へ割り当てるメモリアドレスは、再配置可能である。つまり、実行可能な形のプログラムを作るときに、リンカーがアドレスを変えられる。また、プログラムを動かすときには、プログラムローダーも、マシンの実際のメモリアドレスへ合わせて変更する。図7.11は、その様子を示す。
Figure 7.11

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 7.11 In JavaScript, the variable, in this case, myNumber, has a value that is assigned to a memory address. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図7.11 JavaScriptの変数、この例ではmyNumberは、メモリアドレスに割り当てられた値を持つ。(出典:著作権 Rice University、OpenStax。CC BY 4.0ライセンス)