PROGRAM LoopCountingExample (Input,Output);

(* Illustrate how to count the number of times that nested loops execute *)

VAR
  Count, Inner, Middle, Outer : Integer;

BEGIN
   Count := 0;

   Outer := 1;
   WHILE Outer <= 5 DO     (*Outer loop 1 to 5, so 5 times *)
    Begin
      (*Writeln('Outer= ',Outer);*)
      Middle := 1;

      WHILE Middle <= 20 DO
       Begin
         (*Middle loop from 1 to 20, so 20 times *)
         (*or 20 times for each of 5 outer loops, so 100 times*)
         (*Writeln('Middle= ',Middle);*)
         Inner := 0;

         WHILE Inner < 10 DO
          Begin
            (*Inner loop from 0 to 9, so 10 times *)
            (*or 10 times for each of 20 middle loops, so 200 times *)
            (*or 200 times for each of 5 outer loops, so 1000 times. *)
            (*Writeln('Inner= ',Inner,'  Count= ',Count);*)
            Count := Count + 1;
            Inner := Inner + 1;
          End;
         Middle := Middle + 1;
       End;
      Outer := Outer + 1;
    End;

   Writeln('Count= ',Count);
END.
