Doing Blocks of Code
Program First;
uses Display;
var
Ch:Char;
Begin
ClrScr;
Write('Are you over 21?');
Ch:=ReadKey;
If (Ch='Y') or (Ch='y') then Begin
ClrScr;
Write('Adult features enabled.');
end
else If (Ch='N') or (Ch=n') then Begin
ClrScr;
Write('Regular features enabled.');
end
else Begin
ClrScr;
Write('!! INPUT ERROR !!');
End;
End.
Source Explanation
- Program is a file header - letting Modern Pascal know this is a program. In this case, named "First".
- uses informs Modern Pascal additional run-time libraries to load. In this case, the keyboard and display library "display".
- var is the keyword to inform Modern Pascal that CH is of type CHARACTER.
- Begin ... End. is the main code block - the instructions that produce this algorithm of this program.
Code Block Explanation
- ClrScr stands for "Clear Screen" and erases everything in the command prompt (console, terminal).
- Write displays everything between the apostrophes (known as a string).
- ReadKey makes the program wait until the user presses any key, and stores the KEY character in the variable CH.
- if ... then is a comparison. If the expression CH equals Y or CH equals y then begin multiple instructions.
- else if ... then is another comparison. If the expression CH equals N or CH equals n then debug multiple instructions.
- else if the previous comparisons are not true, this is the catch all - begin multiple instructions.
Take Away(s)
- Notice the semicolon is still on the last IF THEN ELSE instruction in these comparisons.
- However, the instructions between BEGIN and END also end with a semicolon.