Lecture 11: Loops II: While ... Do and Repeat ... Until


While ... Do

The loops in this lecture, while-do and repeat-until are used for repeating things that are not exactly countable. Normally we use this when it is not exactly clear when the loop will finish, for instance because the control variable changes within the loop (as is strictly disadvised in For loops). Also, when we want to loop over something with a floating point type variable we use the while-do and repeat-until loops.

The general format of the while-do loop is

 While condition do 
   Instruction;
 
This structure is repeating the instruction as long as the condition is true. The condition is any condition that results in a boolean value (TRUE or FALSE), as we have discussed in lecture 8. This can be a comparison, or anything else (still information in file?, user pressed a key?, etc).
Note that the structure while-do does not attribute a starting value to any variable like the for-loop was. So, we have to do this ourselves.
Example:
 
program code

PROGRAM WhileExample; 

Var x: real;

begin
  x := 0.0;
  While (x<=1.0) do
    begin
      WriteLn(x:0:1);
      x := x + 0.1;
    end;
end.


  output

  0.0 
  0.1
  0.2
  0.3
  0.4
  0.5
  0.6
  0.7
  0.8
  0.9
  1.0



repeat ... until

The repeat-until structure is very similar to the while-do structure. The repeat-until structure repeats an instruction (or instructions) until a condition is met. The general format is
 
 Repeat
  Instruction;
 until condition
 
Another very important difference between while-do and repeat-until is that with while-do the condition is checked in the beginning of the loop, whereas in repeat-until it is checked at the end. Therefore, the instructions in repeat-until are at least executed once.
Look at the following programs (also an example with a for-loop is included). Only the code with the repeat-until structure has output.
program code

x := 100;
repeat
  writeln('Ajax');
  x := x + 1;
until (x>10);
program code

x := 100;
while (x<=10) do
  begin
    writeln('Ajax');
    x := x + 1;
  end;
program code

for i := 100 to 10 do
  begin
    writeln('Ajax');
  end;
output

Ajax
output

output

Note also the conditions. To make the program do the same thing, we have to make the condition for repeat-until (x>0) the opposite of the condition for while-do (x<=10).

Nesting II

Now that we know all the types of loops, let's take a look at the rules-of-good-behavior related to nesting: Examples of bad code:

for i := 1 to 10 do
  for  k := 1 to 10 do
    begin
      for k := 1 to 10 do 
         writeln('Hello');
    end;
for  k := 1 to 10 do
  begin
    i := k;
    repeat
      j := i-1;
      while (j<10) do
        begin
  end; {end of for}
  i := i + 1;
    until i>20;  {end of repeat-until}
          j := j + 1;
        end; {end of while-do}

Good indentation of your program will 
always avoid such errors.


Summarizing

Three rules for loops:

Quick test:

To test your knowledge of what you have learned in this lesson, click here for an on-line test.

Peter Stallinga. Universidade do Algarve, 15 março 2002