Programming in Pascal: Section 1
Programming in Pascal: Section 1
Contents
SECTION 1 page
SECTION 2 page
SECTION 3
SECTION 4
SECTION 5
SECTION 6
APPENDIX 1
3
PROGRAMMING IN PASCAL
SECTION 1
Introduction
Pascal is an example of a high level computer programming language. There are many other
programming languages and they each have their own advantages and disadvantages.
This course aims to teach you the basics of the language. We'll start with some easy examples
and work our way up. Don't be afraid to experiment and don't take things at face value; ask
questions - it's the best way to learn.
Getting Started
We'll type in a short Pascal program and demonstrate how it works. Pascal recognizes certain
instructions and keywords - we'll type these in capital letters, while anything else will be in small
letters. Make sure you use spaces and punctuation exactly as shown here in the examples.
Example 1
index : INTEGER;
BEGIN
FOR index := 1 TO 10 DO
BEGIN
WRITELN(index)
END
END.
When you've written a program and typed it in, then the next stage is to compile it. A compiler
checks through your program looking for syntax errors - any obvious mistakes that you might
have made, or instructions that Pascal can't understand.
If there are syntax errors, you'll need to correct them before going any further. This is sometimes
the most difficult part of getting a program to work properly - check spellings and punctuation
carefully!
When your program compiles successfully you are ready to run the program and see if it does
what it is supposed to...
Now for some comments - read them carefully and don't be tempted to skip over this part
because it will help you understand what's coming next. We'll look at each line in turn:
All Pascal programs start with the keyword PROGRAM followed by a name and then a semicolon.
The rest of this line is enclosed by curly brackets. Anything in curly brackets is ignored by the
computer - the reason it is there, though, is to act as a comment and to help the reader see what
each part of the program does. When you write your own programs you should always include
such comments.
VAR is short for variable. If your program uses any numbers - or variables - then this is where
we say what they are called and what sort of variables they are. As before, the comment in curly
brackets is ignored by the computer.
index : INTEGER;
...our program uses just one variable. It is called index and it is a whole number, or integer.
Now we come to the start of the main program. It begins, surprisingly enough, with...
BEGIN
Notice that the lines are now set in a little way from the left margin. This is called indenting and
helps to make the program easier to read.
FOR index := 1 TO 10 DO
This is the start of what is called a loop. A loop is an instruction (or set of instructions) which is
carried out repeatedly - this is exactly what computers are good at!
2
© John Brewer 1999
An Introduction to Pascal Programming
BEGIN
WRITELN(index)
...is the only instruction in the loop. It prints out the value of the variable index. Notice, again,
that it has been indented.
END
END.
...marks the end of the program. Note that there is only ever one full stop in a Pascal program,
right at the end.
So far, we've seen that a Pascal program is made up of a number of instructions. The file
containing these instructions is compiled before the program can be run.
Right now, you shouldn't worry too much if some of this seems a little strange. We'll start with
some simple examples and take it from there...
3
© John Brewer 1999
An Introduction to Pascal Programming
Example 2
• choose New from the File menu and type in our next example:
It should be obvious that the result is not what you might have expected. To explain why that is,
let's look more closely at what's happening. Firstly, the program uses three numbers, or variables,
called cost, number and total. The one called number is an integer (a whole number) because it
doesn't make much sense to have, say, 0.7 of a stamp! The other numbers, however - called cost
and total - are not, of course, always going to be whole numbers and so they are described as
type REAL which means they can have a decimal part. That's exactly what has happened and it's
just that the answer is not written in a way that is clear to us. We can solve it though...
This is just a bit of formatting so that, when the number total is printed, 5 spaces will be allowed
for it and there will be 2 numbers after the decimal point. Easy, see...
The program you've just done is different from the first one in that the user had to enter
information when the program is run. In other words, it is interactive as, indeed is most of the
software that you use. An improvement that we might consider making is to allow the user to say
what the price of each stamp is, rather than assume that it's 65c. How could you make the
necessary changes? Here's a clue: you'll need to replace the line cost := 0.65; with two
lines that let the user type in the cost of a stamp.
4
© John Brewer 1999
An Introduction to Pascal Programming
Here's another clue: the two lines will be similar to the part of the program that asks the user to
type in the number of stamps...
Designing Programs
In order to write a program, you first need to decide exactly what it is supposed to do. That might
sound obvious but if you make mistakes at this stage it's unlikely that your program will work
properly. There are four important processes to consider:
• analyze the problem, decide what the program is meant to do and design a solution
• code the program - type in the Pascal code
• test the program - check that it produces correct results
• evaluate the solution - does it do what it's supposed to? Could it be improved?
We'll run through the following example to show you how the process works.
Example 3
A bus company requires a program which will calculate the cost of a journey given the rate per
mile and the length of the journey. For example, if the company charges $0.75 per mile and the
journey is 15 miles then the cost of the journey is 0.75 x 15 = $11.25
1. Design a Solution
Many programs follow a basic design that looks something like this:
1. input information
2. process the information
3. output the results
We'll use that approach to solve our example and start with the following:
5
© John Brewer 1999
An Introduction to Pascal Programming
We now take each of these three steps in turn and write down, in more detail, what each one
does. The first step is to ask the user to enter the information required. What information do we
need? Remember that the user needs to be prompted, or told, what to type in, otherwise they
won't know what to do...
Notice the numbering system - step 1 has been broken down into four separate steps 1.1 to 1.4
The second step is fairly simple and can be described in one line...
So we started with a simple design and refined it, putting in the necessary detail. The complete
design is shown below:
It is important to note that the reason for doing this is not to make extra work for ourselves - it's
so that we can use the design to code the program.
6
© John Brewer 1999
An Introduction to Pascal Programming
The program needs to use three numbers - the number of miles, the rate per mile and the cost of
the journey. We now need to decide i) what to call these numbers, an identifier and, ii) what type
of numbers they are. Here's our suggestion, listed in a table
identifier type
rate REAL
miles INTEGER
cost REAL
Remember that whole numbers are INTEGERs. Numbers which might have a decimal part are
called REAL numbers. So, now for the code...
7
© John Brewer 1999
An Introduction to Pascal Programming
Again, you'll see that the results are not what you perhaps expected.
• edit the last but one and last but two lines to read:
8
© John Brewer 1999
An Introduction to Pascal Programming
Once you have the program working, you should test it to make sure it works properly.
For example, we might draw up a table like the one below and check out various
possibilities. What happens if you enter 6.5 for the number of miles? It is, after all,
supposed to be a whole number.
"The program gives correct results for whole numbers. If the user enters a decimal
number for the number of miles then a 'run-time error' occurs and the program stops. This
also happens if the user enters a letter instead of a number"
4. Evaluation
An evaluation should say whether the program does the job that is was designed for and whether
it could be improved. For example:
"The program correctly calculates the cost of a journey based on the information
provided by the user. Run-time errors occur if the user doesn't enter the correct type of
data and it would be better if the program would allow the user to re-enter incorrect data
instead of just stopping."
9
© John Brewer 1999
An Introduction to Pascal Programming
Example 4
A gardener requires a program which will calculate the length of fencing needed for a
rectangular garden, given that she knows the length and width.
1. Design a Solution
Using a similar approach to the last example, we'll start with the following:
As before, we take each of these steps in turn and write down in more detail what each one does.
What information do we need? How do we calculate the length of fencing?
Step 1 becomes:
Step 2 becomes:
Step 3 becomes:
10
© John Brewer 1999
An Introduction to Pascal Programming
The program needs to use three numbers - the length and width of the garden and the total length
of fencing. What type of numbers should they be - whole numbers or decimal numbers?
• decide what type of numbers you should use - INTEGER or REAL - then copy the table
into your work book and complete it.
identifier type
length
width
total
• open your Pascal editor and type in the code for the program - you can use the last example
as a guide.
• save your program as fencing.pas
• compile and run the program
When you've got the program running, you need to test it.
• draw up a table similar to the one in the previous example and choose some suitable
numbers to test your program
• write down a short summary of the results
4. Evaluation
Have a look at the evaluation for the last example and use it as a guide for this one.
11
© John Brewer 1999
An Introduction to Pascal Programming
Example 5
A bar manager would like a program which calculates the darts score when a player throws three
darts. The program should print out the three scores and their total.
1. Design a Solution
• write down a design for your solution as we have in the last two examples
• show the completed design to your teacher
• as before, draw up a table and choose some suitable numbers to test the program
• write a short summary of the results
4. Evaluation
Example 6
A teacher would like a program that calculates the average mark for students who sit three tests.
Each mark is out of 60 and the program should print out the total mark and the average mark.
1. Design a Solution
• write down a design for your solution as we have in the last three examples
• show the completed design to your teacher
12
© John Brewer 1999
An Introduction to Pascal Programming
• as before, draw up a table and choose some suitable numbers to test the program
• write a short summary of the results
4. Evaluation
Example 7
A newspaper sports editor would like a program which calculates the numbers of points that a
soccer team has. Teams get 2 points for a win, 1 for a draw and 0 if they lose.
1. Design a Solution
• write down a design for your solution as we have in the last few examples
• show the completed design to your teacher
13
© John Brewer 1999
An Introduction to Pascal Programming
• as before, draw up a table and choose some suitable numbers to test the program
• write a short summary of the results
4. Evaluation
Text Variables
All the programs you've done so far have used numeric variables - ie numbers. Each variable is
of a particular type - real or integer - and each has its own name. Variables can be used in
calculations as, for example:
average := total / 3;
In this case, average is calculated as having the value of total divided by 3. There are, however,
other types of variables...
Variables used to represent text are often called string variables. An example would be a variable
containing your name.
Example 8
14
© John Brewer 1999
An Introduction to Pascal Programming
name : STRING[10];
The number 10 in the square brackets means that the string can have up to 10 letters, or
characters, in it. This program, of course, doesn't do a great deal. Apart from anything else, it
prints the same thing every time you run it.
At least this program produces different output depending on what you type in. You'll notice that
a couple of the instructions used WRITE instead of WRITELN. Can you remember what
difference it makes?
15
© John Brewer 1999
An Introduction to Pascal Programming
Semicolons...
You have probably noticed that some lines in your Pascal programs have a semicolon at the end,
and some don't. Sometimes, if you forget to put a semicolon at the end of a line, the compiler
will report that as an error and refuse to compile your program - it's an easy mistake to make and
everyone does it from time to time.
But what are the rules? When is a semicolon required and when does it not matter?
You need to be aware that semicolons are there because they separate one Pascal statement from
the next so that the compiler knows where one statement ends and another begins. Here's an
example from the program you've just done:
The semicolon separates the two WRITELN statements but notice that we do not need a
semicolon at the end of the second last line - END is a keyword, not a statement, so it doesn't
need a semicolon to separate it from the statement on the previous line.
Example 9
This next program is going to ask the user to enter their name, the number of marks they got in a
test and the number of marks that the test is out of. We then calculate the percentage and print
out the result. For example, Steve Shark might have scored 15 out of 20 and this is 75%
1. Design a Solution
16
© John Brewer 1999
An Introduction to Pascal Programming
• as before, draw up a table and choose some suitable data to test the program
• write a short summary of the results
4. Evaluation
Example 10
A youth club runs a disco every Friday, Saturday and Sunday. They sell different flavors of chips
at 65c per bag. The youth club requires a program which adds up the total number of bags of
chips of a particular flavor sold on the three nights and calculates the total money raised.
1 Design a Solution
Step 1 becomes:
Step 2 becomes:
Step 3 becomes:
• look again at what the program needs to do and decide how many variables you need
• what type of variables are they?
• what will you call them?
• if you're not too sure, then see your teacher before you go any further
• use the design above and type in the code for the program
• save the program as chips1.pas
• compile and run the program
18
© John Brewer 1999
An Introduction to Pascal Programming
• write a report on your solution and suggest any improvements that you could make
[From now on we're not always going to ask you to do steps 3 and 4 ie to test the program and
evaluate it. That's not because it isn't important, but because we think you should really do it as
a matter of course and without needing to be asked every time. If it's necessary to look at this
aspect for any particular examples then we will say so. See your teacher if there's any doubt.]
Example 11
The school PE department requires a program which will calculate average times run by students
in the 100yds event. The program should ask for the student's name, their home room class and
the best three times that the student has run. The results printed out should show the student's
name, their home room class and their average time.
1. Design a Solution
• use the previous examples to help you design a solution to the problem.
• show the design to your teacher
19
© John Brewer 1999
An Introduction to Pascal Programming
Decisions, decisions...
Our programs so far have broken down into a list of steps, one after the other. Sometimes,
however, we need to make decisions and choose between two or more options.
Pascal uses the keywords IF, THEN and ELSE when making choices.
Example 12
This is a simple program that reads in a person's age and prints out a message.
Note that only one of the two messages is displayed, depending on the person's age.
20
© John Brewer 1999
An Introduction to Pascal Programming
Example 13
Here's another example. It reads in a person's test mark and prints a 'pass' or 'fail' message.
21
© John Brewer 1999
An Introduction to Pascal Programming
Example 14
This is a game for two players. The program should ask one of the players to enter the name of a
country and then the name of its capital. The screen is cleared and the program should then ask
the other player to enter the capital of the country and print an appropriate message, depending
on whether the answer is right or wrong.
1. Design a Solution
Step 1 becomes:
Step 2 becomes:
22
© John Brewer 1999
An Introduction to Pascal Programming
23
© John Brewer 1999
An Introduction to Pascal Programming
IF answer = capital
THEN
BEGIN
WRITELN('the capital of ', country, ' is ', capital);
WRITELN('well done, you got it right')
END
ELSE
BEGIN
WRITELN('sorry, you got it wrong');
WRITELN('the correct answer is ', capital)
END
END.
• copy and complete this table for the different test data shown
"The program gave correct results when the answer matched the capital exactly - it is
case-sensitive in as much as upper and lower case letters have to be the same. For
example, "Paris" is not the same as "paris". If the user enters a number instead of some
text then the answer will be wrong but it will not cause a 'run-time error'.
If the text is longer than the string - eight characters, in this case - then the word is simply
shortened to fill the space allowed."
24
© John Brewer 1999
An Introduction to Pascal Programming
"The program meets the design specification and produces correct results when tested.
Many improvements are, however, possible. An obvious one would be so that the user
could use either upper or lower case letters - provided, of course, that the spelling was
correct. The program could be developed such that a number of questions were asked and
the user could then be given a score."
Example 15
A large electronic scoreboard in a sports stadium displays the scores of the two teams during a
game. At the end of the game we want it to display a 'congratulations' message to the winning
team. A program is required which asks the user to enter the names of the two teams and the
number of points scored by each. The program will then print out an appropriate message.
1. Design a Solution
• use suitable data to test your program and note the results
There is an obvious flaw in the specification. Does your test data reveal the problem? See your
teacher if you're not sure.
Can you describe the problem with the design and suggest how it might be solved?
Example 16
Those earning over a certain amount have to pay income tax. To keep the figures simple we'll
say that if you earn less than $5000 you pay no income tax, otherwise you pay 25% of anything
over $5000. A person earning $4500 would therefore pay no tax but someone earning $5500
would pay 25% of $500, ie $125 in tax.
See your teacher now if you're not sure how this works.
We would like a program which asks the user to enter the amount earned and which then
calculates the tax paid, if any, and prints out the results.
1. Design a Solution
A loop is part of a program that gets repeated a number of times. The very first Pascal program
you did had a loop in it. Loops can be conditional in which case they repeat until something
happens - a condition is met - or they can be unconditional, in which case they are repeated for a
fixed number of times. You've already seen an example of an unconditional loop - look back at
the first program in this tutorial. The loop starts with...
FOR index := 1 TO 10 DO
... and the instruction(s) to be repeated go between a BEGIN and an END statement:
BEGIN
WRITELN(index)
END
In this case the variable called index is a counter which controls the loop - it starts at 1 and goes
up to 10
26
© John Brewer 1999
An Introduction to Pascal Programming
i) Unconditional Loops
Recall Example 6 where a teacher wanted to read in three marks and calculate the average. Now
consider what would happen if there were 10 or 20 or 200 marks...
...well this is what loops are designed for and if we know in advance how many times the loop is
to be repeated, then so much the better!
Here's a different design for the program. As each mark is read in, we're going to keep a running
total:
Step 1 becomes:
Step 2 is:
Step 3 is:
27
© John Brewer 1999
An Introduction to Pascal Programming
PROGRAM unconditional;
VAR
mark : INTEGER;
total : INTEGER;
average : REAL;
BEGIN
total := 0;
FOR counter = 1 TO 10 DO
BEGIN
WRITE ('type in the exam mark: ');
READLN (mark);
total := total + mark
END;
average := total / 10;
WRITELN('the total is: ');
WRITELN('the average is: ', average)
END.
• there are two mistakes in the program which means that it won't compile until you find
them - can you see what they are?
• there are two more mistakes that you should be able to find when you run the program -
can you fix them?
28
© John Brewer 1999
An Introduction to Pascal Programming
Here's a better design that lets the user decide how many students there are:
Notice in this example what is meant by a 'running total' and notice also the line in the program
that updates this total.
29
© John Brewer 1999
An Introduction to Pascal Programming
Now for a conditional loop. This is where you don't know in advance how many times you're
going to have to go round the loop - you need to loop until something happens to stop the loop,
or until a condition is met.
VAR
number : INTEGER;
guess : INTEGER;
BEGIN
number := 5;
REPEAT
WRITE('enter a number between 0 and 10: ');
READLN(guess);
UNTIL guess = number;
WRITELN('what took you so long?')
END.
As you can see, the loop is repeated until the number typed in is 5.
Example 17
We'll extend the idea now so that one user types in a number and the other person tries to guess
it...
1. Design a Solution
30
© John Brewer 1999
An Introduction to Pascal Programming
Step 1 becomes:
Step 2 becomes:
2.1 repeat
2.2 prompt player 2 for guess
2.3 read in guess
2.4 until guess is correct
2.1 repeat
2.2 prompt player 2 for guess
2.3 read in guess
2.4 until guess is correct
3 print message
31
© John Brewer 1999
An Introduction to Pascal Programming
Example 18
We'd like now to extend the design of the previous program and add one or two new features. It
would be helpful if the program could give some clues and say whether the guess is too big or
too small. It would also be helpful if the program could count the number of guesses and print
the total at the end. We'll start our new design just like the original one:
1. Design a Solution
Step 1 becomes:
Step 3 becomes:
2.1 repeat
2.2 prompt player 2 for guess
2.3 read in guess
2.4 print clue
2.5 update number of guesses
2.6 until guess is correct
Step 2.4 needs some clarification - note how the numbering system works...
Step 3 becomes:
32
© John Brewer 1999
An Introduction to Pascal Programming
2.1 repeat
2.2 prompt player 2 for guess
2.3 read in guess
2.4.1 if guess > number
2.4.2 then
2.4.3 print "guess is too big"
2.4.4 if guess < number
2.4.5 then
2.4.6 print "guess is too small"
2.5 update number of guesses
2.6 until guess is correct
As before, you should note that the whole point of designing the program is to make it easy to
code. Check that the design above corresponds with the program below.
VAR
number : INTEGER;
guess : INTEGER;
total : INTEGER;
BEGIN
WRITE('enter the number to guess ');
READLN(number);
CLRSCR;
REPEAT
WRITE('enter your guess ');
33
© John Brewer 1999
An Introduction to Pascal Programming
READLN(guess);
IF guess > number
THEN
WRITELN('guess is too big');
IF guess < number
THEN
WRITELN('guess is too small');
total := total + 1;
UNTIL guess = number;
WRITELN('well done');
WRITELN('you took ', total, ' guesses')
END.
This completes Part 1 of the tutorial, so well done if you've followed everything so far.
34
© John Brewer 1999
An Introduction to Pascal Programming
SECTION 2
Introduction
You've now covered some of the basic programming ideas and are now in a position to think in
more detail about how best to make programs work for you. Solving problems can be a
frustrating business but programming is meant to be fun too - remember that everyone makes
mistakes and you shouldn't be surprised if your program doesn't work first time...
Procedures, procedures...
All of the programs you've done so far have been fairly short. For more complex tasks though,
clearly, the code for the programs is going to be more complicated and much longer. For this
reason, and also because it's good practice, most programs are broken down into smaller sections
called procedures. A procedure, then, is a section of code which does a particular job. This part
of the course is a rather simplified treatment of some of the issues involved in using procedures.
We should note that, as with most things, there is more to the topic than meets the eye but a more
detailed treatment can be left for later.
Example 19
To illustrate the use of procedures, we'll use Example 10, the design for which is shown below:
Notice that, as with many designs, we start with a basic scheme with a small number of steps - in
this case, three - and then take each of them in turn and write a more detailed description.
We wish to split our program into smaller 'chunks', or procedures, so we'll decide now that there
will be three - one for each of the main steps 1, 2 and 3. Have a look at what each one does then
decide on a name for the procedure in the same way that you choose names for variables.
step name
VAR
flavor : STRING[10];
friday : INTEGER;
saturday : INTEGER;
sunday : INTEGER;
bags : INTEGER;
sales : REAL;
36
© John Brewer 1999
An Introduction to Pascal Programming
The error message that you get isn't really surprising since Pascal knows nothing of your three
procedures! We need to describe what each procedure does. Let's take get_info first and remind
ourselves of the detailed description for step 1 as given in Example 10
Our procedure will do just what is required for each of these steps. The code is shown below.
Notice again how closely the instructions follow the detailed design. The code for the procedure
is slotted in before the start of the main program so that we now have:
37
© John Brewer 1999
An Introduction to Pascal Programming
Again the error message should come as no surprise since we have only entered one of the three
procedures so far. Step 2 looked like this and the procedure only has a couple of instructions in
it...
38
© John Brewer 1999
An Introduction to Pascal Programming
• it's your turn now to write the code for the procedure results and put it into the program
• save the complete program
• compile and run it and then show it to your teacher
39
© John Brewer 1999
An Introduction to Pascal Programming
Example 20
Each of these three steps will become a procedure. Let's choose names for them as follows:
step name
As before, though, if you try to compile this you'll get error messages because there are three
procedures - get_info, get_answer and message - which we haven't yet coded. We'll now put in
the first two of these and you can complete the other one yourself:
40
© John Brewer 1999
An Introduction to Pascal Programming
41
© John Brewer 1999
An Introduction to Pascal Programming
Example 21
We'll go through a complete example now to make sure we've got the hang of things so far.
A company gets orders from its customers and gives a discount of 10% on orders worth more
than $200. Whatever the total comes to, sales tax at 5% has to be added. A typical bill, or
invoice, might look like this:
value = 220.00
discount = 22.00
subtotal = 198.00
sales tax = 9.90
total = 207.90
If the value is less than $200 then the discount would, of course, be zero but you would still need
to add sales tax.
See your teacher now if you're not sure how this works.
1. Design a Solution
[HINT: already you should see that the main program looks like being made up of three
procedures]
Step 1 is straightforward...
42
© John Brewer 1999
An Introduction to Pascal Programming
Step 3 is simply
So if we use get_value, calc_total and invoice for the procedure names, the outline of the
program will look like this:
If you've got this far and understood the course material then, once again, you've done well.
There's more to learn, of course, but you've done much of the hard work!
44
© John Brewer 1999