第7章高水準プログラミング言語
Sequential Execution順次実行
Executing statements in the order in which they appear, sequential execution, is a linear ordering of statements in which one statement directly follows another. This is the default flow of control; it is automatic. An example of sequential execution would be the following:
英語のヒント
文が現れる順序で実行することを、順次実行という。これは、一つの文の直後に次の文が続く、直線的な順序である。標準の制御の流れであり、自動的に行われる。順次実行の例を次に示す。
int myAge = 35;
String myName = "Johnathan";
boolean isStudent = true;These statements will execute one at a time in the order in which they are written.
これらの文は、書かれた順序で、一つずつ実行される。
A code block is a statement that consists of one or more statements that are structured in a sequential group and delineated as such. This is necessary so that an entire group of commands may be executed as a single sequential structure. In the C-like languages, opening and closing curly braces ({ }) are usually used to denote the beginning and end of a code block. We can modify the preceding example into a code block as follows in Java:
英語のヒント
コードブロックは、一つ以上の文を順次実行する集まりにまとめ、そのまとまりとして区切った、一つの文である。命令の集まり全体を、一つの順次構造として実行するために必要になる。Cに似た言語では、通常、開き波括弧と閉じ波括弧({ })で、コードブロックの始まりと終わりを表す。先ほどの例は、Javaでは次のようにコードブロックへ変えられる。
{
int myAge = 18;
String myName = "Johnathan";
boolean isStudent = true;
}Note that variables defined within code blocks only exist in the context of that code block, which makes it possible to manage the scope of variables within code more precisely. It is a best practice in programming to indent code contained within structures so that the code is both readable and maintainable.
英語のヒント
コードブロック内で定義した変数は、そのブロックの範囲内だけに存在することに注意しよう。これにより、コード内での変数のスコープを、より正確に管理できる。構造の内部にあるコードを字下げし、読みやすく、保守しやすくするのが、プログラミングの望ましい方法である。
Selection選択
Decision making in programming is called selection. A decision-making construct allows us to choose from one or many paths of execution in a program. To see how they work, one must understand the concept of conditional expressions.
英語のヒント
プログラミングで判断を行うことを、選択という。判断のための構文によって、プログラム内の一つ、または多数の実行経路の中から選べる。その動作を理解するには、条件式の概念を知る必要がある。
Conditional Expressions
条件式
Sometimes called a Boolean expression, a conditional expression evaluates the Boolean values of true or false. Based on a Boolean result, a computer may decide which path of execution to take. Some examples of conditional expressions follow:
真偽式とも呼ぶ条件式は、真偽値のtrueまたはfalseを求める。この真偽の結果に基づき、コンピューターは、どの実行経路を取るかを決められる。次に、条件式の例を示す。
•
value1 == value2• value1 == value2
•
value1 != value2• value1 != value2
•
value1 < value2• value1 < value2
•
value1 <= value2• value1 <= value2
Selection Statements
選択文
The simplest form of selection statement is a decision structure known as a one-way branch, as displayed in Figure 7.12. Most languages represent a selection statement that is a one-way branch with a construct known as an if statement. Other languages may use a slightly different syntax. In Java, a relatively simple if statement using a code block looks like this (it is recommended to use code blocks for single-line selection statements to avoid errors subsequently when/if more statements are added):
英語のヒント
最も単純な選択文は、図7.12に示す、一方向分岐という判断構造である。多くの言語は、一方向分岐の選択文を、if文という構文で表す。ほかの言語では、少し異なる構文を使う場合がある。Javaでコードブロックを使った比較的単純なif文は、次のようになる。選択文が一行だけでも、後で文を追加した場合の誤りを避けるため、コードブロックを使うことを勧める。
short temp = 90;
(…)
if (temp >= 95) {
System.out.println("It is hot");
}
attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典
Figure 7.12 This unified modeling language (UML) activity diagram represents a selection statement that is a one-way branch. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図7.12 この統一モデリング言語(UML)のアクティビティ図は、一方向分岐の選択文を表す。(出典:著作権 Rice University、OpenStax。CC BY 4.0ライセンス)
The only type of conditional statement that a language needs is the simple one-way branch and any number of them may be placed in sequential order to carry out any decision-making task. However, a variety of decision structures have evolved into different flavors that make programs easier to code, more readable, and more maintainable. Its logic is shown in Figure 7.13.
英語のヒント
言語に必要な条件文の種類は、単純な一方向分岐だけであり、これを必要な数だけ順に並べれば、どんな判断の仕事でも行える。ただし、コードを書きやすく、読みやすく、保守しやすくするために、さまざまな判断構造が、異なる形へ発展してきた。その論理を図7.13に示す。

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典
Figure 7.13 A unified modeling language (UML) activity diagram representing a selection statement that is a two-way branch. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図7.13 二方向分岐の選択文を表す、統一モデリング言語(UML)のアクティビティ図。(出典:著作権 Rice University、OpenStax。CC BY 4.0ライセンス)
In Java, this might be coded with a two-way branch using an if…else statement:
Javaでは、if…else文による二方向分岐で、次のように書ける。
int myNumber = 4;
if (myNumber % 2 == 0)
{
System.out.println("The number is even.");
}
else
{
System.out.println("The number is odd.");
}Sometimes decisions might result in three or more possible pathways. In the C-like languages, this situation might be coded with a multi-way branch using an if…else…if statement:
判断の結果、可能な経路が三つ以上になる場合もある。Cに似た言語では、if…else…if文を使った多方向分岐で、この状況を書ける。
if (myGrade >= 90)
{
System.out.println("You got an A.");
}
else if (myGrade >= 80)
{
System.out.println("You got a B.");
}
else if (myGrade >= 70)
{
System.out.println("You got a C.");
}
else
{
System.out.println("You need to work harder.");
}Many programming languages have employed an additional selection statement for times when decisions result in three or more possible pathways. This is known as a switch or case statement, and it can be much more readable and maintainable than many if statements strung together. This looks like the following:
英語のヒント
多くのプログラミング言語は、判断から三つ以上の経路が生じる場合のために、追加の選択文を用意している。switch文またはcase文といい、多数のif文をつなぐよりも、はるかに読みやすく、保守しやすくできる。次のように書く。
switch (myGrade)
{
case 90: System.out.println("You got an A.");
break;
case 80: System.out.println("You got a B.");
break;
default: System.out.println("You need to work harder.");
break;
}