Introduction to the Pascal Programming Language

© 1998 by Dr. Thomas W. MacFarland -- All Rights Reserved


     Instructions:   Review the following program and see if you can 
                     predict the outcome by completing a "pencil 
                     trace" of the code.  

                     Program output is provided at the end of this 
                     page.

     Compiler:       The program was prepared using Standard Pascal 
                     on a UNIX-based host computer.


% cat PASCAL/intro_pencil-14.p
{ Introduction to the Pascal Programming Language
  Dr. Thomas W. MacFarland
  intro_pencil-14.p       
}

{ The purpose of this program is to:

  1.  Demonstrate the structure of a while/do loop.

  2.  Demonstrate a counter variable that increments in
      value with each iteration.

  3.  Demonstrate a counter variable that decrements in
      value with each iteration.
}

program Demonstrate_a_While_Do_Loop;

var A, B : integer;

begin

   A := 3;                           {What is the value of A as it }
   while A < 8 do begin              {ends the 5th loop?  What     }
     write('  A = ', A :4);          {happens, because of this     }
     A := A + 1;                     {value?                       }
   end; writeln;
   writeln('  A = ', A :4); writeln; {What is the value of A now?  }

   B := 7;                           {What is the value of B as it }
   while B > 2 do begin              {enters the loop for the 3rd  }
     write('  B = ', B :4);          {time?  What happens, because }
     B := B - 3;                     {of this value?               }
   end; writeln;
   writeln('  B = ', B :4); writeln; {What is the value of B now?  }

end.  { intro_pencil-14.p      } 
% pc PASCAL/intro_pencil-14.p
% a.out
  A =    3  A =    4  A =    5  A =    6  A =    7
  A =    8

  B =    7  B =    4
  B =    1


Please send comments or suggestions to Dr. Thomas W. MacFarland

There have been visitors to this page since February 1, 1999.