Delphi: Programming loops


Often it occurs that instructions have to be repeated as long as a number of conditions are satisfied.
Look at the following flowchart, where proc1,2,3 are statements and procedures
and quest1,2,3 are questions to decide where to continue the program based on different conditions.



These loops are simple to program in the old-fashioned way: labels and goto statements.
In a structured way however complications may arise, see flowchart


The loop is repeated as long as the conditions in the while statement are satisfied.
Problem is, that when returning to the while statement after a loop, the path taken through
quest1..quest3 is not obvious.
This results in complex condition checking.

A tric is to introduce a boolean variable GO as the sole condition of the while statement.
GO is set true before entering the while statement.
Just after this statement GO is set to false.
This implies that when a condition is not satisfied, the while statement is entered with GO=false
and an exit from the loop takes place.
Only at the end of the loop, implying that all conditions where met, GO is set true,
which continues the loop.


Here is what the program may look:
  ...
  var GO : boolean;
  ...
  begin
   ....
   GO := true;
   while GO do
    begin
     GO := false;
     //...procedures
     if (condition1) then
      begin
       //..procedures
       if (condition2) then
        begin
         //..procedures
         if (condition3) then
          begin
           //....
           GO := true;
          end;//cond3
        end;//cond2
      end;//cond1
    end; //while
...
end;