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

{ The purpose of this program is to:

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

  2.  Demonstrate multiple values for a case statement by
      the use of the Boolean "or" operator.
}

program Demonstrate_Case_1;

var Letter_Grade: char;

begin
  writeln('What letter grade do you hope to earn? '); 
  writeln;
  writeln('Enter A for letter grade A');
  writeln('Enter B for letter grade B');
  writeln('Enter C for letter grade C');
  writeln('Enter D for letter Grade D');
  writeln('enter F for letter Grade F');
  writeln;
  write('Enter the letter grade you hope to earn: ');
       {Note:  For the purpose of this pencil-trace program,   }
       {assume that user enters a lower case "a" at this point.}
  read(Letter_Grade);
  writeln;

  case Letter_Grade of 
    'A' , 'a' : writeln('Letter grade = A');
    'B' , 'b' : writeln('Letter grade = B');
    'C' , 'c' : writeln('Letter grade = C');
    'D' , 'd' : writeln('Letter grade = D');
    'F' , 'f' : writeln('Letter grade = F');

    '+', '-', '*', 
    '=', '<', '>', 
    '/'       : writeln ('Oops ... you selected a math or operator symbol.');

    '!', '@', '#', 
    '$', '%', ', 
    '&', '_', '~', 
    '`', '\', 
    '|'       : writeln ('Oops ... you selected a special character.');

    ',', ';', '(', 
    ')', '{', '}', 
    '[', ']'  : writeln ('Oops ... you selected a punctuation mark.');

    '0', '1', '2', '3',   
    '4', '5', '6', '7',   
    '8', '9'  : writeln ('Oops ... you selected a digit.');

    'E', 'e', 'G', 'g', 'H', 'h', 'I', 'i', 'J', 'j',
    'K', 'k', 'L', 'l', 'M', 'm', 'N', 'n', 'O', 'o', 
    'P', 'p', 'Q', 'q', 'R', 'r', 'S', 's', 'T', 't',
    'U', 'u', 'V', 'v', 'W', 'w', 'X', 'x', 'Y', 'y', 
    'Z', 'z'  : writeln ('Oops .. you selected an out-of-range character.');

  end;  (* case *)

end.  {advanced_pencil-18.p}
%  pc PASCAL/advanced_pencil-18.p
%  a.out
What letter grade do you hope to earn? 

Enter A for letter grade A
Enter B for letter grade B
Enter C for letter grade C
Enter D for letter Grade D
enter F for letter Grade F

Enter the letter grade you hope to earn: a

Letter grade = A
%  a.out
What letter grade do you hope to earn? 

Enter A for letter grade A
Enter B for letter grade B
Enter C for letter grade C
Enter D for letter Grade D
enter F for letter Grade F

Enter the letter grade you hope to earn: A

Letter grade = A
%  a.out
What letter grade do you hope to earn? 

Enter A for letter grade A
Enter B for letter grade B
Enter C for letter grade C
Enter D for letter Grade D
enter F for letter Grade F

Enter the letter grade you hope to earn: g

Oops .. you selected an out-of-range character.
%  a.out
What letter grade do you hope to earn? 

Enter A for letter grade A
Enter B for letter grade B
Enter C for letter grade C
Enter D for letter Grade D
enter F for letter Grade F

Enter the letter grade you hope to earn: G

Oops .. you selected an out-of-range character.
%  a.out
What letter grade do you hope to earn? 

Enter A for letter grade A
Enter B for letter grade B
Enter C for letter grade C
Enter D for letter Grade D
enter F for letter Grade F

Enter the letter grade you hope to earn: \

Oops ... you selected a special character.
%  a.out
What letter grade do you hope to earn? 

Enter A for letter grade A
Enter B for letter grade B
Enter C for letter grade C
Enter D for letter Grade D
enter F for letter Grade F

Enter the letter grade you hope to earn: ,

Oops ... you selected a punctuation mark.
%  a.out
What letter grade do you hope to earn? 

Enter A for letter grade A
Enter B for letter grade B
Enter C for letter grade C
Enter D for letter Grade D
enter F for letter Grade F

Enter the letter grade you hope to earn: 9

Oops ... you selected a digit.

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.