Teaching Programming I

Copyright © by Mark Baker 1996

Copyright waiver

Return to Contents


Introduction

I begin by talking about sequence, selection and repetition. This is something that I reinforce continually, for the first few weeks of the course and return to occasionally beyond that.

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:

Sequence, Selection, Repetition

All Pascal programs consist of just these three elements. Sequence is obvious, leaving only two things to learn about, selection and repetition. (If only it was that simple...!)

Basic Structures

Basic data types are introduced (integer, real, character and string) and the need to declare variables is discussed. Note that a string is in fact a data structure, however, Turbo Pascal treats it in a similar way to the atomic data types.

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!

  • Repetition - There are 3 structures for creating a loop, FOR, REPEAT UNTIL and WHILE DO

  • FOR loops are used when you know, before you start the loop, how many times you want to go around it. Note the use of the assignment operator ( := ). This is also a good time to introduce compound statements, using BEGIN and END pairs.

    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:
    Fred Fred Fred Mary
    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:
    Fred Mary Fred Mary Fred Mary

  • REPEAT UNTIL loops are perhaps the most intuitive, behaving as they are read, eg. Repeat these instructions until you have processed all the data, or Repeat looking at each item in the list until you find the one you are looking for. Repeat loops are always passed through at least once, since the exit test is made at the bottom of the loop.

    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.

  • WHILE DO loops are not understood as well. Essentially they are the same as REPEAT loops, except that the exit test is made at the top of the loop, so you may never pass through the it. As with a FOR loop, only the next statement after the WHILE DO is included within the loop, unless a BEGIN END pair is used.

    Students don't seem to like WHILE DO loops and they don't use them very often.

  • Selection - There are 2 structures for making choices, IF THEN and CASE OF

  • IF THEN statements come in various different flavours, IF THEN, IF THEN ELSE, IF THEN ELSE IF and so on, both with and without BEGIN END pairs.

    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.

  • CASE OF statements can often be used to replace complex IF statements, for example:
    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;
    

    Return to Contents

    Teaching Programming II
    Teaching Programming III
    Teaching Programming IV


    Author: Mark Baker, e-mail mbaker@rmplc.co.uk
    Last revision: 2nd November 1996