Advanced Pascal Programming Language Problems

© 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/advanced_pencil-19.p
{ Advanced Pascal Programming Language Exercises
  Dr. Thomas W. MacFarland
  advanced_pencil-19.p
}

{ The purpose of this program is to:

  1.  Demonstrate the use of case statements in Standard      
      Pascal.

  2.  Demonstrate the odd and/or even functions.
}

program Demonstrate_Case_2;

var      n : integer;
    Number : boolean;

begin
  repeat 
    writeln;
    write('Enter an integer, or -1 to quit: ');
    readln(n);     {The readln statement "reads in" keyboard entry.} 
    writeln;

    Number := odd(n + 1);            
    case Number of 
      TRUE  : writeln('Number was even.');
      FALSE : writeln('Number was odd.');
    end;  (* case *)
    writeln('Number = ', n);
    writeln('Print true if even and false if odd: ', Number);
  until n = -1;
end.  {advanced_pencil-19.p}
%  pc PASCAL/advanced_pencil-19.p
%  a.out

Enter an integer, or -1 to quit: 3

Number was odd.
Number =          3
Print true if even and false if odd: false

Enter an integer, or -1 to quit: 2

Number was even.
Number =          2
Print true if even and false if odd: true

Enter an integer, or -1 to quit: 1

Number was odd.
Number =          1
Print true if even and false if odd: false

Enter an integer, or -1 to quit: 0

Number was even.
Number =          0
Print true if even and false if odd: true

Enter an integer, or -1 to quit: -1

Number was odd.
Number =         -1
Print true if even and false if odd: false

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.