Another important construct for controlling the flow of a program is the loop. Loops cause a set of statements to be executed repeatedly. Of course, if we want to execute a set of statements repeatedly, we need to answer two questions. First, which statements in our program should be included in the loop? Second, when should the loop end?

The first question is answered by the keywords that identify a loop. The keyword While indicates the beginning of the loop and the keyword EndWhile signals the end of the loop. All the statements that come between these two keywords are called the loop body and are executed repeatedly.

The second question is answered by a test that is included at the beginning of the loop. The test consists of a Boolean expression that is either true or false. Before each loop iteration, the test is performed. If the result is true, the loop is executed. If the result is false, the loop ends, and the program continues with the first statement following the EndWhile keyword. The table below shows the general structure of a loop.

While (<boolean expression>)

   <body of the loop>

EndWhile

To understand the usefulness of loops, consider the following situation. Suppose that you are the teacher of a class, and you have just given an exam to 5 students. Now you want to write a simple program to calculate the average test grade in the class. If you store the test scores in an array of Real variables, you can compute the average grade with the following statement:

avgGrade := (grade[1] + grade[2] + grade[3] + grade[4] + grade[5]) / 5

Another approach is to use a loop to sum the test scores in the array. During each iteration of the loop, another test score is added to the sum, and when all the scores are added, the sum is divided by the number of students. The instructions for this approach look like this:

Integer STUDENTS := 5
Integer
count := 1

Real
sum := 0

While
(count <= STUDENTS)
   sum := sum + grade[count]
   
count := count + 1
EndWhile

avgGrade
:= sum / STUDENTS

The second approach certainly seems more complex that our first one, but it has an important advantage. As the size of the class increases, this program is very easy to modify. All we need to do is change the value of the constant STUDENTS to reflect the new class size. But with our first approach, we will need to explicitly write out all the array elements. Imagine what would happen if we used this approach for a class of 100 students! Clearly we would prefer to use a loop to sum the class scores with such a large class size.

The animation below shows how our loop would execute with a class size of five students. Use the buttons at the bottom of the animation to control the execution of the program. As you watch the animation, notice how the flow of control is altered by the loop construct.

In the animation, you saw that the flow of control always returned to the beginning of the loop after the body of the loop was executed. You also saw that before each loop iteration, the loop test was executed. Once the count exceeded the number of students in the class, the loop test failed, and the flow of control moved to the first statement after the EndWhile. Whenever you use loops in a program, you must design your loop test carefully to ensure that your loop will end. If your test never becomes false, your program will be caught in an infinite loop.