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

{ The purpose of this program is to:

  1.  Demonstrate the use of multiple variable types in loops:  
      integer, real, boolean, char.

  2.  Demonstrate the selection process for these variables while 
      in the loop.

  3.  Demonstrate increment and decrement of an integer counter   
      variable and a real counter variable.
}

program Demonstrate_Loops_With_Mixed_Variable_Types;

var I_1      : integer;
    I_2      : integer;
    I_3      : integer;
    I_Sum    : integer;
    R_1      : real;
    R_Sum    : real;
    Alpha    : char;
    B_1      : boolean;

begin

   I_1 := 2;  
   I_2 := 5;   

   for I_3 := I_1 to I_2 do
     begin     
       write('  I_3 = ',I_3 :3);
     end; writeln;

   for I_3 := I_1 to I_2 do     
     begin
       if I_3 <= 4 then 
       write('  I_3 = ',I_3 :3);
     end; writeln;

   I_Sum := 0;

   writeln;
   for I_3 := I_1 to I_2 do
     begin        
       I_Sum := I_Sum + 2;
       write('  I_3 = ', I_3 :3, '  and I_Sum  = ', I_Sum :3); writeln;
     end; writeln;

   for I_3 := I_3 downto -1 do           
     begin 
       R_Sum := R_Sum + 2.5;
       writeln('  I_3 = ', I_3 :3, '  and R_Sum  = ', R_Sum :5:2);
     end; writeln;

   write('Alphabetical Characters = ');
   for Alpha := 'd' to 'p' do        
     begin
       write(Alpha);
     end; writeln;

   write('Alphabetical Characters = ');
   for Alpha := 'p' downto 'd' do        
     begin
       write(Alpha);
     end; writeln;

   writeln;
   B_1 := true;
   for I_3 := 0 to 3 do            
     begin
       if I_3 > 2 then B_1 := false;
         writeln('  I_3 = ', I_3 :3, '  and B_1    = ', B_1 :6);
     end; writeln;

end.  { intro_pencil-19.p      } 
% pc PASCAL/intro_pencil-19.p
% a.out
  I_3 =   2  I_3 =   3  I_3 =   4  I_3 =   5
  I_3 =   2  I_3 =   3  I_3 =   4

  I_3 =   2  and I_Sum  =   2
  I_3 =   3  and I_Sum  =   4
  I_3 =   4  and I_Sum  =   6
  I_3 =   5  and I_Sum  =   8

  I_3 =   5  and R_Sum  =  2.50
  I_3 =   4  and R_Sum  =  5.00
  I_3 =   3  and R_Sum  =  7.50
  I_3 =   2  and R_Sum  = 10.00
  I_3 =   1  and R_Sum  = 12.50
  I_3 =   0  and R_Sum  = 15.00
  I_3 =  -1  and R_Sum  = 17.50

Alphabetical Characters = defghijklmnop
Alphabetical Characters = ponmlkjihgfed

  I_3 =   0  and B_1    =   true
  I_3 =   1  and B_1    =   true
  I_3 =   2  and B_1    =   true
  I_3 =   3  and B_1    =  false


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

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