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-07.p
{ Introduction to the Pascal Programming Language
  Dr. Thomas W. MacFarland
  intro_pencil-07.p       
}

{ The purpose of this program is to:

  1.  Demonstrate the use of integer variables and how these 
      variables can appear when printed using simple format 
      operations.  

  2.  Demonstrate addition and subtraction with integers.

  3.  Demonstrate multiplication and division with integers.

  4.  Demonstrate the mod and div operators with integers.

  5.  Call attention to the need for extreme care when 
      determining the value of an algorithm as variables 
      change values during the operation of the program. 
}
program Demonstrate_Integer_Math;

var A,B,C,D : integer;
    E       : real;

begin

   A := 1;                   
   B := A + 2;               
   C := A + B;
   D := 2 * C;               
   E := D / C;                 

   write('  A = ',A:4); write('  B = ',B:4); write('  C = ',C:4);
   write('  D = ',D:4); write('  E = ',E:4:2); writeln;

   D := C div B;             
   E := D / C;                 

   write('  A = ',A:4); write('  B = ',B:4); write('  C = ',C:4);
   write('  D = ',D:4); write('  E = ',E:4:2); writeln;

   D := C mod B;             
   E := D / C;                 

   write('  A = ',A:4); write('  B = ',B:4); write('  C = ',C:4);
   write('  D = ',D:4); write('  E = ',E:4:2); writeln;

   D := (A * B) div (D + C); 
   E := D / C;                 

   write('  A = ',A:4); write('  B = ',B:4); write('  C = ',C:4);
   write('  D = ',D:4); write('  E = ',E:4:2); writeln;

end.  { intro_pencil-07.p      } 
% pc PASCAL/intro_pencil-07.p
% a.out
  A =    1  B =    3  C =    4  D =    8  E = 2.00
  A =    1  B =    3  C =    4  D =    1  E = 0.25
  A =    1  B =    3  C =    4  D =    1  E = 0.25
  A =    1  B =    3  C =    4  D =    0  E = 0.00

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

There have been [an error occurred while processing this directive] visitors to this page since February 1, 1999.