Learning Pascal - The Basics



For this page, I will be using the Oxford Pascal version.

Load Pascal on your trusy 64. After you get the initial message you will be able to follow along.

The basic structure of Pascal is built on blocks. This is clearly understandable by the following simple program:

10 begin
20 write('Hello')
30 end.


In Oxford Pascal, after you enter in the first line, the computer automatically numbers the next one for you. If you try to enter the program above, be sure to enter it in exactly the way it is. Line 10 is the beginning of this program itself and is a required command for your program. Notice the 'end.' command at line 30. This signals the end of your program. As we proceed, you will notice a variant of this command. Just know that the last 'end' must have the period. BEGIN and END are also used in looping blocks. Later on, you'll see one of these.

Line 20 could be considered the equivalent of the PRINT command in BASIC or PRINTF in C. Text must be between apostrophies, which must be enclosed with parenthesis. This line is what prints "Hello" to the screen after the program has been compiled and run.

To compile a program, you type 'r' and press RETURN. This tells Pascal that you are done entering in code and are ready to compile and run it. If there are zero errors, it will (after printing some stuff on the screen) execute the program and print out "Hello" to the screen.

This was a super simple program! Now, for a little more...

Type NEW and press RETURN. This will clear the previous program from memory.

Enter in the following:

10 begin
20 write(1+2);
30 write(2*2)
40 end.


- Line 10 is the beginning of our program.
- Line 20 prints out our addition problem. Notice the semi-colon...this is required and tells Pascal that this is where line 20 ends. Line 30 does not need one since it is the last statement before 'end.'.
- Line 30 prints out a result from multiplying 2 * 2.
- Line 40 is the end of the program.

O.k., what if we want to use a variable in the previous program instead of specific numbers?

Here is how it is done...

10 var a,b,:integer;
20 begin
30 a:=1;
40 b:=2;
50 write(a+b);
60 write(b*b)
70 end.

If you are not familiar with Pascal (or ALGOL) you will probably be asking "why the the 'var...' line before 'begin'? This is because in Pascal, variables must be defined before the beginning of the program. Here is a breakdown of the program, line-by-line:

- Line 10 defines variables "the VAR command" a and b. Then we tell Pascal that a and b are variable types known as integer. More on data types later. Then we tell Pascal that we have reached the end of this line by placing a ';' at the end (also known as a 'statement separator').
- Line 20 is the beginning of the program. We do not need a semi-colon after a begin.
- Line 30 is where we assign a numerical value to 'a'. Notice when we assign a value, there is a ':' and '=' between the variable and the number.
- Line 40 similar to line 30.
- Line 50 print out result of adding the value contained in a and b.
- Line 60 print out the result of multiplying the contents of b by itself.
- Line 70 end of program.

More on text...

O.k., what if we wanted to get something from the user - similar to the INPUT statement in BASIC? In Pascal, this is handled differently. The concept of string handling is taken in a different way with Pascal. Here is a simple program to illustrate how it is done...

10 program getsomeinfo (input,output);
20 var firstname,lastname,greet:char;
30 begin
40    writeln('Please enter your first name');
50    readln firstname;
60    readln lastname;
70    write('Welcome to programming Pascal, 'firstname,lastname)
80 end.