Back to DFS's Turing Page


User-Controlled Repetition with the end-exit Loop

Computers are excellent at doing the same operation over and over again. Sometimes the user knows exactly how many times s/he wants an action to be done, in which case the for loop is usually the preferred programming structure. However, often the user does not know or may be interrupted or is overwhelmed by the work or is too lazy to count up how many times a task needs to be done. When dealing with a fickle user, it is best to provide the user with as much control over the process as possible. This can be accomplished by using a end-exit loop, which allows the user to continue repeating a task until s/he doesn't want to do it any more.

We will use another modification of the area-of-a-triangle program to illustrate this. The problem, pseudocode and flowchart are as follows:

The Problem: Write a program to calculate the areas of as many triangles as the user would like to have done, with the user providing the dimensions.

The Pseudocode:
1. Simple introduction and instructions;
2. Do the main job repeatedly
    a) Find out the dimensions;
    b) Calculate the area;
    c) Print out the results;
    d) Ask whether to continue or not
       until the user says no.

A end-exit loop contains the statements that are to be repeated in a compound statement. Indentation is used to help the programmer visualize the structure of the loop.

When using a end-exit loop, a variable is used to determine whether the loop should be repeated or not. This is known as the "loop control variable" and is given the name KeepGoing in this program. It is tested/checked at the end of each iteration of the loop. This means that the loop will always be executed at least once. The variable KeepGoing is of type char, and when it has a value of n or N, the loop is terminated.

Sometimes the user starts a program and then realizes that it is the wrong one. The technique described here would require the user to enter the dimensions for at least one triangle before being able to terminate the program. Using the end-exit loop for this task may be giving the user too much credit. The beginning-exit loop provides a method to allow the user to exit the program without needing to enter any data.

% Calculating the areas of a user-controlled number of triangles
var b, h: int %Base & Height
var A: real %Area
var KeepGoing: char %Loop control variable
%------------Introduction & Setup------------------
put "This program calculates and prints the areas of triangles"
put "one after another after you enter their dimensions."
put "When asked to, type in a dimension and hit the ENTER key."
loop
   put ""
   %-------------------Inputting Data------------------
   put "What is the length of the triangle's base? "..
   get b
   put "What is the triangle's height? "..
   get h
   %---------------------Calculation------------------
   A := b * h / 2
   %-------------------Print the result---------------
   put "The area of a triangle with a base of ", b, " units and"
   put "a height of ", h, " units is ", A:0:1, " square units."
   put ""
   %-------------------User Control------------------
   put "Do you want to do another? (Y/N) "..
   get KeepGoing
   exit when (KeepGoing = 'N') or (KeepGoing = 'n')
end loop

© 1997-2004 DFStermole
HTMLified: 29 Dec 99
Last Modified: 16 Feb 04