Copyright © by Mark Baker 1996
An algorithm is a list of instructions which when carried out completes some task or solves a particular problem. Examples of algorithms include a recipe, a knitting pattern, the instructions to program your video recorder and computer programs.
A computer program is a list of instructions, which are executed by a computer. There are only three types of structure which make up a typical computer program and they are:
The students are shown the basic structure of a Pascal program:
program <Program Name>;
uses <Library 1>,<Library 2>,...; {The libraries you will use are listed here.}
const max=10; {Program constants are declared here}
type ....; {Don't worry about this section, it can be left out}
var i: integer; {Main program variables are declared here}
name: string;
{All your procedures and functions will come next, in this section}
{------------------------------------------------------------------------------------------------------------------------}
procedure <Procedure Name1>;
begin
.
.
end;
{------------------------------------------------------------------------------------------------------------------------}
procedure <Procedure Name2>;
begin
.
.
end;
{------------------------------------------------------------------------------------------------------------------------}
.
. {Other procedures and functions}
.
{------------------------------------------------------------------------------------------------------------------------}
begin {<--Start of Main Program. When the program is run, it starts here}
.
. {The main program is usually a menu}
.
.
end. {Note that the program ends with a full stop}
The students have seen the basic structure of a program and the next step is to start them with the ubiquitous "Hello World!" program.The basic Hello World! program, which simply writes the message "Hello World!" on the screen, is useful in showing what a minimal program must contain. All programs must build on from here. At the same time, I cover logging on to the network, loading the programming editor, compiling and running the program. This program also introduces their first procedure, writeln.
The programming structures are now introduced individually. The students first take notes, which explain how each one is written, with a general form and several specific examples as illustration. I then give them a simple programming task to complete, that involves using just that one structure, for example "Write a program that will output your name on the screen five times." I like to work it so that I break up the note taking with short practical exercises, like this:
Notes on FOR
Write a program using a FOR statement
Notes on REPEAT UNTIL
Write a program using a REPEAT loop, etc.
The initial exercises are very trivial, deliberately so, as it is important that students clearly understand these basic building blocks. It is especially important not to leave students feeling that they do not really understand what is happening at this stage, some find this introductory work quite daunting. The focus is totally on the particular statement/structure being studied, with no peripheral demands to cloud the issue.
Others do find some of these exercises very trivial and it is also important for them not to lose interest. This is easily dealt with, as the basic exercises can be readily extended and made more complex, for those students who are obviously coping well. I may give them additional requirements, or I may show the quicker students how to do additional things, such as how to change text and background colours and leave them to embelish their work. This has the knock-on benefit that this knowledge usually gets passed on to the rest of the class, who see what their peers are producing and want to be let in on the secret, without it having to be formally taught!
for i:=1 to 3 do
write('Fred ');
write('Mary ');
Only the first line following the FOR is included in the loop. This will produce:
for i:=1 to 3 do
begin
write('Fred ');
write('Mary ');
end;
The BEGIN and END pair allows us to have several statements within the loop. This will produce:
It will be necessary to talk about conditions at this stage; you may want to limit your students to simple conditions, such as:
count:=0;
REPEAT
.
.
.
count:=count+1;
UNTIL count=5; {<--- simple condition}
Or, depending on the ability of your students, you may wish to take this opportunity to discuss compound conditions such as:
count:=0;
REPEAT
readln(ch);
.
.
count:=count+1;
UNTIL (count=5) or (ch='Q'); {<--- compound condition}
The difference between the assignment operator ( := ) and the equals sign ( = ) needs careful emphasis and reinforcement, as it is a common cause of confusion. The brackets are also very important in compound conditions. The third thing that needs to be emphasised is the importance of indenting structures (eg. starting to type the contents of a loop a couple of spaces beyond the margin formed by the REPEAT and the UNTIL. Whilst this makes absolutely no difference to the way in which the code is executed, it does make the program much more readable. The structure of a program can be readily understood if it has been indented well. If a program is not indented then it makes it very difficult to read. Students should be encouraged to adopt a consistent style when programming.
Students don't seem to like WHILE DO loops and they don't use them very often.
These all need to be covered, although it is probably better to introduce the different forms gradually.
Care must be taken in the use of semi-colons as incorrect use can prematurely end an IF statement, to the confusion of the compiler! Do not put a semi colon before an ELSE or an ELSE IF.
if MenuChoice = 1 then procedure1
else if MenuChoice = 2 then procedure2
else if MenuChoice = 3 then procedure3;
...can be replaced with...
case MenuChoice of 1: procedure1; 2: procedure2; 3: procedure3; end;Note the use of the meaningful variable name, MenuChoice. Pascal gives you a lot of freedom with variable names and by using meaningful ones, programs become much more readable. This is important, not only for others who are trying to work out what has been done, but also for the original author - it is surprising how quickly you forget why you wrote what you did.
CASE statements can also have an ELSE section, although I personally have never had need to use one. BEGIN and END pairs can also be used.
case MenuChoice of
1: begin
procedure1a;
procedure1b;
end;
2: procedure2;
3: procedure3
else procedure4;
end;
Teaching Programming II
Teaching Programming III
Teaching Programming IV