touque.ca > Education Commons > Programming: Program control > Repetition

Program control

Repetition

In order to effectively handle error conditions, and to allow the program’s user to perform more than a single task, it’s common for a program to have one or more sections of code which are executed more than once. The decision to repeat a section of code is based on conditions encountered during processing. This type of program control is called repetition.

Conditional loops

For example, if a program’s user provides invalid data in response to a request that she choose a menu option, it’s necessary to point out the error, and request that the user revise her choice.

Following is a program fragment which is able to perform these tasks. It uses a loop, delimited by the keywords loop and end loop, to repeatedly present a prompt for a menu choice, exiting the loop body only when a valid choice is obtained.

% Example 1:

% Get a valid menu choice.
loop

   put "Your choice [a or b]? " ..
   get menuChoice

   exit when menuChoice = "a" or menuChoice = "b"

   % If we got this far, the menu choice is invalid. Display a diagnostic warning,
   % before repeating the prompt for a valid menu choice.
   put "\"", menuChoice, "\" is not a valid choice."
   put "Please choose \"a\" or \"b\"."
   put skip

end loop

% rest of processing appears here…

Example 1 uses a boolean expression to control the exit from the loop body. The boolean expression follows the keywords exit when, and has a value of true or false, depending on the value stored in menuChoice.

If the boolean expression has the value true, when the computer reaches the exit statement, then the program is “free” of the loop, and continues execution with the statement which follows end loop.

If the boolean expression has the value false, when the computer reaches the exit statement, then the computer continues—in sequence—through the remaining statements of the loop body. Once the computer reaches the bottom of the loop body, it jumps to the first statement, and sequential execution continues from there.

If the boolean expression never has the value true, then the computer will never exit the loop body. A loop from which the computer can never exit is called an “infinite loop”; in general, this is a situation which should be avoided.

(There’s an inside joke, at Apple Inc., about an infinite loop.)

Counted loops

A common programming task is to repeat a section of code a fixed number of times. To accomplish such a task, we use counted loops. The central feature of a counted loop is that we know exactly how many times the loop body will execute.

As the name implies, a counted loop involves a counter; this counter is called the control variable. The control variable is declared, initialized, and controlled by the loop itself. The control variable may not be declared by the programmer: it is entirely under the control of the loop, and its scope is limited to the loop’s body.

A counted loop is introduced by the keyword for, a programmer-selected identifier for the control variable, an initial value of the control variable, and the final value of the control variable. The initial and final values must be ordinal (integers or characters) and may be constants or variables.

In Example 2, we see a simple for loop with just one statement in the loop body. This loop’s control variable is named positive_integer. When the loop is executed, the control variable is declared and initialized with the value 1. The loop then tests whether the value of the control variable is greater than the final value (in this case: 10). If it is, the loop is done: execution continues with the statement which follows end for. If it’s not, the loop body is executed, the value of the control variable is incremented, and then the value of the control variable is again compared to the final value. If the control variable is now greater than the final value, execution continues with the statement which follows end for; if not, the value of the control variable is incremented …

% Example 2:

for positive_integer : 1 .. 10

   put positive_integer

end for

Important vocabulary

accumulator:
an integer or real variable used to calculate the total of a set of values; an accumulator is incremented value by value
counter:
an integer variable used to record how many items have been processed, or how many events have transpired; a counter is usually incremented by one
sentinel value:
a watched-for value whose appearance indicates that a special event has occurred; sentinel values are often used to indicate that the end of a set of regular values has been reached

touque.ca > Education Commons > Programming: Program control > Repetition