Back to DFS's Pascal Page


User-Controlled Repetition with the Repeat-Until 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-To-Do 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 Repeat-Until 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 Repeat-Until loop contains the statements that are to be repeated in a compound statement. BEGIN and END are not used to delimit the compound statement, because the reserved words REPEAT and UNTIL are sufficient to let the compiler know what statements constitute the loop. Indentation is used to help the programmer visualize the structure of the loop.

When using a Repeat-Until 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. The While-Do loop provides a method to allow the user to exit the program without needing to enter any data.

program TriangleAreaRepeat (input, output);
   var
      b, h: integer; {Base & Height}
      A: real; {Area}
      KeepGoing: char; {Loop control variable}
   begin
      {------------Introduction & Setup------------------}
      writeln('This program calculates and prints the areas of triangles');
      writeln('one after another after you enter their dimensions.');
      writeln('When asked to, type in a dimension and hit the ENTER key.');
      writeln;
      Repeat
         {-------------------Inputting Data------------------}
         write('What is the length of the triangle''s base? ');
         readln(b);
         write('What is the triangle''s height? ');
         readln(h);
         {-------------------Calculation------------------}
         A := b * h / 2;
         {-------------------Printing Results------------------}
         writeln('The area of a triangle with a base of ', b:0, ' units and');
         writeln('a height of ', h:0, ' units is ', A:0:2, ' square units.');
         writeln;
         {-------------------User Control------------------}
         write('Do you want to do another? (Y/N) ');
         readln(KeepGoing)
      Until (KeepGoing = 'N') or (KeepGoing = 'n')
   end.

© 1997-99 DFStermole
HTMLified: 29 Dec 99