For loops are generally the most common type of loop we will deal with. The next most common is the while loop. While loops execute until a Boolean expression evaluates to false. For example, consider the program we created previously that reads in a single digit and returns the word for that digit. What if we want to continue prompting the user for a single digit if he enters a letter instead of a digit. The following while loop will achieve this. Try running it and enter a letter instead of a number several times before entering a digit. The program will continue to execute Readln(Num) until it receives a number. Try running it:
{$APPTYPE CONSOLE}
program Workspace;
var
  Num : Char;
begin
  Num := ' ';
  while not ((Num >= '0') and (Num <= '9')) do
  begin
    Write('Enter a single number to get its name: ');
    Readln(Num);
  end;
  case Num of
    '0': Writeln('Zero');
    '1': Writeln('One');
    '2': Writeln('Two');
    '3': Writeln('Three');
    '4': Writeln('Four');
    '5': Writeln('Five');
    '6': Writeln('Six');
    '7': Writeln('Seven');
    '8': Writeln('Eight');
    '9': Writeln('Nine');
  end;
  Readln;
end.
Of course, we had to start this program with the line Num := ' ' because when the program starts the value of Num is considered undefined. In fact, the value is something completely random. So, there is a chance that the command Readln(Num) will never execute because Num could randomly be set to an actual number when the program starts. To see what happens, change Num := ' ' to Num := '7' and run the program. This would obviously result in a bug in the program that happens during some runs and not during others. In fact, failure to initialize variables is often the cause of hard to diagnose bugs, but this will be discussed more in detail later. For now, say we wanted to ensure that Readln(Num) executes at least one time without having to initialize Num. Object Pascal provides a mechanism to do this called the repeat loop. Repeat loops are identical to while loops except that they execute a statement once before testing the exit condition. Thus, the following code does exactly what the while loop code does except there is no need to initialize Num because Readln executes before the exit condition is tested. Note also that in the while loop, the loop exits when the exit condition is false. In a repeat loop, the loop exits when the exit condition is true.
{$APPTYPE CONSOLE}
program RepeatLoop;
var
  Num : Char;
begin
  repeat
    Write('Enter a single digit number to get its name: ');
    Readln(Num);
  until (Num >= '0') and (Num <= '9');
  case Num of
    '0': Writeln('Zero');
    '1': Writeln('One');
    '2': Writeln('Two');
    '3': Writeln('Three');
    '4': Writeln('Four');
    '5': Writeln('Five');
    '6': Writeln('Six');
    '7': Writeln('Seven');
    '8': Writeln('Eight');
    '9': Writeln('Nine');
  end;
  Readln;
end.
Often, the choice of for, while, or repeat is rather arbitrary. Consider the following for loop:
{$APPTYPE CONSOLE}
program Workspace;
var
  K : Integer;
begin
  For K := 0 to 10 do
    Writeln(K);
  Readln;
end.
This loop could have been written as a while loop:
{$APPTYPE CONSOLE}
program Workspace;
var
  K : Integer;
begin
  K := 0;
  while K <= 10 do
  begin
    Writeln(K);
    Inc(K);
  end;
  Readln;
end.
In another example, using Break, the following repeat loop can also be rewritten as a while loop:
{$APPTYPE CONSOLE}
program Workspace;
var
  Num : Char;
begin
  repeat Readln(Num) until (Num >= '0') and (Num <= '9');
end.
And as a while loop:
{$APPTYPE CONSOLE}
program Workspace;
var
  Num : Char;
begin
  while True do
  begin
    Readln(Num);
    if (Num >= '0') and (Num <= '9') then Break;
  end;
end.
So there are multiple ways to write loops. One can easily see why some languages have as few as 1 type of loop. You should choose the syntax that makes your code the most readable. We recommend using for loops and while loops only. This is because most languages support a for loop and a while loop at least (not all support a repeat loop). Use a for loop when there are a known number of items to loop through. Use a while loop when you want to repeat something until a condition becomes false.