Repeating Actions

Loop structures allow the computer to choose repeat a sequence of actions until a goal is achieved.

In Chapter 6, we learned how to use an if-then-else or if-then structure to decide which action to perform. In this chapter, we will learn how to create a block of statements that can be executed several times in succession. We do this using a repetition structur (also called a loop), which is one of the fundamental control structures supported by most imperative and object-oriented programming languages.

A repetition structure (or loop) allows a group of statements to be executed several times in succession. There are three important repetition structures: a loop repeats an action for every object in a collection of objects, a loop that is controlled by the state of the objects in the program, and a loop that is controlled by a counter (usually a number). In this chapter, we are going to focus on just one kind of loop, one that is controlled by the state of the objects in the program. This happens to be the most general and most fundamental kind of repetition structure in many programming languages.

There are two major parts to every repetition structure, the body and the controlling condition. These two parts provide a way to classify loops.

The block of statements that can be executed repeatedly is called the body of the loop. Each time that the statements in the body are executed is called a trip (or iteration) through the loop, and the number of times the body is executed is called the trip count.

The controlling condition is a condition that is checked to determine whether to make a trip through the body or terminate the loop. The controlling condition is rechecked after each trip through the body of the loop.

One criterion for classifying loops is based on when the controlling condition is checked relative to the first trip through the body. In a pretest loop, the controlling condition is always checked before the body can be executed for the first time. In a posttest loop, the controlling condition is not checked until after the first trip through the body. In either case, the condition is checked after each trip through the body to determine whether or not to make another trip.

A second criterion for classifying loops is based on whether a true condition or a false condition leads to a trip through the body. In a while loop, a true condition leads to a trip through the body, but a false condition terminates the loop. In an until loop, a true condition terminates the loop, but a false condition leads to a trip through the body. The difference between the while and until loops is summarized in this table:

While Until
ConditionActionConditionAction
true Make a trip through body true Terminate the loop
false Terminate the loop false Make a trip through body

Combining these two criteria, we can define four broad categories of loops: pretest while, pretest until, posttest while, and posttest until. Few programming languages provide all four of these (most only provide two, or even one!), but the most common form that is supported in virtually every imperative and object-oriented programming language is the pretest while loop. We'll focus exclusively on pretest while loops in the remainder of this chapter.

Pre/Post While/Until JavaPythonVisual BasicOthers
PretestWhile nearly all others
PretestUntil COBOL, but few others
PosttestWhile many others
PosttestUntil Pascal, and several others

Since the pretest while loop is the most common repetition structure across imperative and object-oriented languages, we will take a closer look at it.

The while-loop control structure.

The figure above shows a generic pretest while loop and uses arrows to show the order in which statements are executed and the condition is checked.

Java syntax for a while loop.

The figure above shows the Java system for a pretest while loop in Java. There are three important things to observe about the syntax.

  1. The condition must be in parentheses.

  2. There is no semicolon after the parentheses.

  3. The while structure is not a method, which means that we do not send it as a message to a Jeroo object.

There are three important things to observe about the coding style.

  1. Braces are used to define the beginning and end of the body. Always include them.

  2. The braces are aligned with the start of the word while.

  3. The statements between the braces should be indented (we use 4 spaces).

Example (pretest while structure)

Assume that a Jeroo named Kim is not standing on a flower, but there is a line of flowers ahead. Have Kim pick all of those flowers, and stop as soon as there is no flower directly ahead. After picking all of the flowers, Kim should turn to the left.

while (kim.seesFlower(AHEAD))
{
    kim.hop();
    kim.pick();
}

kim.turn(LEFT);