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

{ The purpose of this program is to:

  1.  Demonstrate the readln statement as a means of user-generated
      data entry.  

  2.  Demonstrate the issue of case sensitivity for char variables. 

  3.  Demonstrate data entry issues when using a repeat/until loop.
}

program Demonstrate_Readln_In_a_Repeat_Until_Loop; 

var Alpha   : char;         
    A, B, C : integer; 

begin

  A := 10;
  B := A + 2;
  C := A + B; 

  repeat
    writeln;
    writeln ('  ***************************************'); 
    writeln ('  Do you want to go on with this program?'); 
    writeln ('  If so, type g at the prompt ==>        '); 
    write ('  *********************************** ==>  '); 
    readln (Alpha)
  until (Alpha = 'g');

  {  Assume that, by mistake, the user enters an upper case G
     at the prompt.  Predict what the user would see on the 
     screen after the RETURN/ENTER key is pressed.
  }

  {  Then, compile the program again and assume that the users 
     correctly enters a g at the prompt on the first try.  Predict 
     what the user would see on the screen after the RETURN/ENTER 
     key is pressed.
  }

  writeln;
  write('  A = ', A : 8);
  write('  B = ', B : 8);
  write('  C = ', C : 8); writeln;

end.  { intro_pencil-20.p      } 
% pc PASCAL/intro_pencil-20.p
% a.out

  ***************************************
  Do you want to go on with this program?
  If so, type g at the prompt ==>        
  *********************************** ==>  G

  ***************************************
  Do you want to go on with this program?
  If so, type g at the prompt ==>        
  *********************************** ==>  h

  ***************************************
  Do you want to go on with this program?
  If so, type g at the prompt ==>        
  *********************************** ==>  g

  A =       10  B =       12  C =       22

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.