第7章高水準プログラミング言語
Iteration反復
Also known as looping, iteration is going around and re-executing sequences of statements. One of the great strengths of computers is their ability to repeat actions over and over. Iteration structures allow us to write statements to be repeated just one time with the repetition monitored by flow of control structures.
英語のヒント
ループとも呼ぶ反復は、一連の文を巡り、再実行することである。何度も同じ動作を繰り返せることは、コンピューターの大きな強みの一つである。反復構造を使えば、繰り返したい文を一度だけ書き、制御フローの構造によって、その繰り返しを管理できる。
The number of iterations is controlled by conditional expressions much like selection. The type of conditional statements used to control loops are categorized as either condition-controlled or count-controlled. In the condition-controlled scenario, the loop will continue to iterate until a condition is met. In a count-controlled situation, the loop repeats a specific number of times.
英語のヒント
反復回数は、選択とよく似て、条件式によって制御する。ループを制御する条件文の種類は、条件制御型と回数制御型に分類できる。条件制御型では、ある条件が満たされるまで繰り返す。回数制御型では、特定の回数だけ繰り返す。
Iteration structures may also be categorized as top-tested or bottom-tested. In a top-tested loop, a condition is set at the entrance to the code block. If the condition is met, the loop executes and will continue to repeat until the condition is false. Note that if the condition in a top-tested loop is never true, the loop will not execute at all (for example “while True:” as the top-test of a loop in Python will result in the loop being executed forever).
英語のヒント
反復構造は、前判定と後判定にも分類できる。前判定ループでは、コードブロックの入口に条件を置く。条件が満たされれば実行し、条件が偽になるまで繰り返す。前判定の条件が一度も真にならなければ、ループはまったく実行されないことに注意しよう。たとえば、Pythonでループの前判定を「while True:」とすると、そのループは永久に実行される。
In the bottom-tested loop, the sentinel, the expression that sets the condition at the entry or exit of a loop for continued iteration, is at the exit after the code block. A loop such as this is guaranteed to execute at least one time. The sentinel decides if the loop runs again. It is useful to guarantee at least one repetition of a loop.
英語のヒント
後判定ループでは、番兵を、コードブロックの後の出口に置く。番兵とは、繰り返しを続けるための条件を、ループの入口または出口に設定する式である。このようなループは、少なくとも一度は必ず実行される。もう一度実行するかどうかを、番兵が決める。少なくとも一度の反復を保証したいときに役立つ。
As with selection statements, most languages have a variety of structures to choose from and just like in selection, there is only one that is necessary to do any kind of iteration: the top-tested condition-controlled loop. Its logic is represented by Figure 7.14. Note that it is assumed in this example that the variable “raining” can change as a result of an external event in reaction, say, to the output of a sensor that detects rain and issues a callback to an event handler (not shown here) that changes the value of the “raining” variable to being true.
英語のヒント
選択文と同じく、ほとんどの言語はさまざまな反復構造を持つが、どんな反復を行うにも必要なのは、前判定の条件制御ループ一つだけである。その論理を図7.14に示す。この例では、「raining」変数が外部イベントによって変化しうると仮定していることに注意しよう。たとえば、雨を検出するセンサーの出力に反応して、イベントハンドラー(ここには示していない)をコールバックで呼び出し、ハンドラーが「raining」の値をtrueへ変える。

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典
Figure 7.14 This unified modeling language (UML) activity diagram represents a while loop. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図7.14 この統一モデリング言語(UML)のアクティビティ図は、whileループを表す。(出典:著作権 Rice University、OpenStax。CC BY 4.0ライセンス)
In most modern languages, a top-tested loop is known as a while loop. Its syntax in C# is as follows:
現代の言語の大半では、前判定ループをwhileループという。C#の構文は次のとおりである。
Boolean happy = true;
Boolean rainin = false;
while (happy == true)
{
Console.out("I am happy");
if (raining == true) // raining value controlled by event handler
{
Console.out("I am frowning");
happy = false;
}
}The sentinel in this loop is the variable happy, and if this condition remains true the loop will iterate.
このループの番兵はhappy変数であり、その条件がtrueのままなら、ループは繰り返す。
This type of loop is non determinative, a loop for which we cannot predict the number of iterations (condition controlled). A determinative loop that is predictable in that its number of iterations will execute exactly five times and could be constructed in C# as follows:
英語のヒント
この種類は、反復回数を予測できない、回数不定のループ(条件制御型)である。反復がちょうど五回と予測できる、回数が定まったループなら、C#で次のように作れる。
int count = 1;
while (count <= 5)
{
Console.out("I am smiling");
count = count + 1;
}In most modern languages a bottom tested loop is known as a do…while loop as highlighted in Figure 7.15. It’s syntax in C# is as follows:
現代の言語の大半では、図7.15のような後判定ループをdo…whileループという。C#の構文は次のとおりである。
Boolean happy = true;
Boolean raining = false;
do
{
Console.out("I am smiling");
if (raining == true) // raining value controlled by event handler
{
Console.out("I am frowning");
happy = false;
}
} while (happy == true)The sentinel in this loop is still the variable happy; the loop will iterate at least once, and if this condition remains true the loop will continue to iterate.
このループでも番兵はhappy変数である。少なくとも一度は繰り返し部分を実行し、条件がtrueのままなら、さらに反復を続ける。

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典
Figure 7.15 This unified modeling language (UML) activity diagram represents a do…while loop. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図7.15 この統一モデリング言語(UML)のアクティビティ図は、do…whileループを表す。(出典:著作権 Rice University、OpenStax。CC BY 4.0ライセンス)
The sentinel in this loop is again the variable happy; it will iterate at least once and will continue if this condition remains true. This type of loop is non determinative because we cannot predict when it will rain and, therefore, how many times it will execute. We could construct it in such a way as to make it count-controlled. For count-controlled loops, another style is a for loop. This structure is top-tested and count-controlled. It is concise, elegant, easy to maintain, and efficient. An example of a for loop in PHP is as follows:
英語のヒント
このループの番兵もhappy変数である。少なくとも一度は実行し、条件がtrueのままなら反復を続ける。いつ雨が降るか、したがって何回実行するかを予測できないので、このループは回数不定である。回数制御になるように作ることもできる。回数制御ループの別の形式が、forループである。これは、前判定で、回数により制御する構造である。簡潔で、整っており、保守しやすく、効率がよい。PHPのforループの例を次に示す。
for ($count = 1; $count <= 5; $count = $count +1) {
print("the count is: "", $count); }The rules are as follows: inside the parentheses, the first expression is executed only once at the first entrance to the loop. The second expression is the sentinel and it is tested at all iterations of the loop. The last expression is executed at the bottom of each iteration and sets the stage for the next iteration.
英語のヒント
規則は次のとおりである。括弧内の最初の式は、初めてループへ入るとき、一度だけ実行する。二番目の式は番兵で、繰り返すたびに判定する。最後の式は、各反復の末尾で実行し、次の反復の準備をする。