MPL Documentation

How to handle when the user did not press Y,y or N,n?

Program First;

uses Display;


   Write('Are you over 21?');
   If (Ch='Y') or (Ch='y') then Write('ADULT')
   else If (Ch='N') or (Ch=n') then Write('MINOR')
   else Write('!! INPUT ERROR !!');

Source Explanation

  1. Program is a file header - letting Modern Pascal know this is a program. In this case, named "First".
  2. uses informs Modern Pascal additional run-time libraries to load. In this case, the keyboard and display library "display".
  3. var is the keyword to inform Modern Pascal that CH is of type CHARACTER.
  4. Begin ... End. is the main code block - the instructions that produce this algorithm of this program.

Code Block Explanation

  1. ClrScr stands for "Clear Screen" and erases everything in the command prompt (console, terminal).
  2. Write displays everything between the apostrophes (known as a string).
  3. ReadKey makes the program wait until the user presses any key, and stores the KEY character in the variable CH.
  4. if ... then is a comparison. If the expression CH equals Y or CH equals y then display ADULT.
  5. else if ... then is another comparison. If the expression CH equals N or CH equals n then display MINOR.
  6. else if the previous comparisons are not true, this is the catch all - display !! INPUT ERROR !!.

Take Away(s)

  • Notice the semicolon is now on the last instruction in these comparisons.