第7章高水準プログラミング言語
Documentation and Comments文書化とコメント
All programming languages allow a programmer to document their code to improve code readability. This is a best practice in programming and a prospective job applicant may not get a job without demonstrating documentation skills.
英語のヒント
どのプログラミング言語でも、プログラマーは、コードを読みやすくするための説明を書き残せる。これはプログラミングの推奨事項であり、就職を目指す応募者は、文書化の能力を示さなければ、採用されない場合がある。
Documentation is carried out by using a structure called a comment, a container used to hold documentation in code. A comment begins with a defined symbol or set of symbols of the language followed by the comment text itself. Comments may be single line or multiline depending upon the symbol used. Single line comment symbols indicate that whatever follows on the line is ignored by the compiler or interpreter. Multiline comments have an opening and a closing symbol or set of symbols. Examples are as follows:
英語のヒント
文書化には、コード内に説明を収める入れ物である、コメントという構造を使う。コメントは、その言語で定めた記号や記号の組で始まり、続いてコメント本文を書く。使う記号に応じて、単一行または複数行のコメントになる。単一行コメントの記号は、その行で後に続くものをすべて、コンパイラーやインタープリターが無視することを示す。複数行コメントには、開始と終了の記号、または記号の組がある。次に例を示す。
// single line comment in C#
int myAge = 21;
int myAge = 21; // another single line comment in C#
/* this is a multiline comment in JavaScript myAge will be used to hold the age of a
student */
int myAge = 21;Other languages may use different syntax for comments; in Python the symbol is the pound sign (#). In BASIC it is the REM keyword, which is short for “remark.”
英語のヒント
ほかの言語では、コメントの構文が異なる場合がある。Pythonの記号は、ナンバー記号(#)である。BASICでは、「remark」の略であるREMキーワードを使う。
HLL Expressions and StatementsHLLの式と文
Commands in HLLs are structured as statements, and statements are made up of expressions. We have learned that an expression in a programming language evaluates to a value. Examine the following statements in a C-like language:
英語のヒント
HLLの命令は文として構成し、文は式から成る。プログラミング言語の式を評価すると値になる、と学んだ。Cに似た言語の、次の文を調べよう。
int a = b + c * d;If it is given that b = 4, c = 6, and d = 2, a simple scan of the statement would have the first addition statement evaluate to 10, with that being used to evaluate the resulting statement of 10 * 2, giving a value of 20. However, this calculation is incorrect because expression evaluation within statements follows order of precedence, the rules that determine the order in which operators in statements are evaluated. Therefore 6 * 2 will be evaluated first, giving the value 12, and then 4 + 12 will be evaluated, giving a final result of 16.
英語のヒント
b = 4、c = 6、d = 2とすると、文を単純に左から追えば、最初の加算を10と評価し、その値を使って10 * 2を求め、結果が20になるように見える。しかし、この計算は誤りである。文の中の式の評価は、演算子を評価する順序を定めた、優先順位の規則に従うからである。したがって、最初に6 * 2を評価して12を得て、次に4 + 12を評価し、最終結果は16になる。
Parentheses may be used to modify the order of precedence in statements. Expressions within parentheses are always evaluated first. If we were to rewrite the example:
英語のヒント
括弧を使うと、文の評価の優先順位を変えられる。括弧の中の式は、必ず最初に評価する。この例を次のように書き直すと、
int a = (b + c) * d;we would come up with the result of 20. Table 7.8 shows the results of various expressions when following the rules of precedence and applying parentheses to them.
英語のヒント
結果は20になる。表7.8は、優先順位の規則に従った場合と、括弧を適用した場合の、さまざまな式の結果を示す。
Expression 式 | Value 値 | Parenthesized Expression 括弧を付けた式 | Value 値 |
|---|---|---|---|
5 + 2 * 4 | 13 | (5 + 2) * 4 | 28 |
10 / 5 - 3 | -1 | 10 / (5 - 3) | 5 |
8 + 12 * 2 - 4 | 28 | 8 + 12 * (2 - 4) | -16 |
4 + 17 % 2 - 1 | 4 | (4 + 17) % 2 - 1 | 0 |
6 - 3 * 2 + 7 - 1 | 6 | (6 - 3) * (2 + 7) - 1 | 26 |
Table 7.8 Expressions and Their Values
表7.8 式とその値
Some HLL expressions that use the logical operators (Java, C++, C#, JavaScript) do not have to be completely evaluated for their results to be known, a concept called short circuiting. Referring to the truth tables of the logical && and || operators, we can see that in a && operation, the only way a result of true can be obtained is if both operands are true. Therefore, if the first operand is false, the expression does not have to be evaluated further—it is false. This is short circuiting. Here is an example in JavaScript:
英語のヒント
論理演算子を使うHLL(Java、C++、C#、JavaScript)の式には、すべてを評価しなくても結果が分かるものがある。この考え方を短絡評価という。論理演算子&&と||の真理値表を見ると、&&の結果がtrueになるのは、両方のオペランドがtrueの場合だけだと分かる。したがって、最初のオペランドがfalseなら、それ以上評価する必要はなく、式の結果はfalseである。これが短絡評価である。JavaScriptの例を次に示す。
if (x == y || today == "Tuesday") {
// do_something
}If the operand on the left side of the logical operator || evaluates to true, then the expression is true, and the expression on the right side of the || operator does not need to be evaluated. Short circuiting increases efficiency and performance.
論理演算子||の左側のオペランドがtrueと評価されれば、式全体はtrueになり、||の右側の式を評価する必要はない。短絡評価は、効率と性能を高める。
Flow of Control制御の流れ
As we learned in Introduction to Data Structures and Algorithms, we need to define the steps to be taken to solve a problem or complete a task. The order in which, or if, the statements of a program are executed is called flow of control. By default, program statements execute in sequential order from an established starting point; however, the flow of control can be modified. This is necessary in order to have the ability to model the situations of the real world that our algorithms represent.
英語のヒント
「データ構造とアルゴリズム入門」で学んだように、問題を解決したり、タスクを終えたりするには、行う手順を定める必要がある。プログラムの文を、どの順序で実行するか、あるいは実行するかどうかを、制御の流れという。標準では、プログラムの文は、定められた開始地点から順に実行するが、制御の流れは変えられる。これは、アルゴリズムが表す現実世界の状況を、モデル化できるようにするために必要である。