Programming Tools & Techniques                                                                             _        James Leong Mook Seng

Programming Techniques 

Programming technique is a way of

  • Unstructured programming,
  • procedural programming,
  • modular programming

Unstructured Programming

Usually, people start learning programming by writing small and simple programs consisting only of one main program. Here ``main program'' stands for a sequence of commands or statements which modify data which is global throughout the whole program. The main program directly operates on global data. It can be illustrated:

This programming techniques provide tremendous disadvantages once the program gets sufficiently large. For example, if the same statement sequence is needed at different locations within the program, the sequence must be copied. This has lead to the idea to extract these sequences, name them and offering a technique to call and return from these procedures.

Procedural Programming

With procedural programming it is possible to combine returning sequences of statements into one single place. A procedure call is used to invoke the procedure. After the sequence is processed, flow of control proceeds right after the position where the call was made.

 

With introducing parameters as well as procedures of procedures ( subprocedures) programs can now be written more structured and error free. For example, if a procedure is correct, every time it is used it produces correct results. Consequently, in cases of errors you can narrow your search to those places which are not proven to be correct.

Now a program can be viewed as a sequence of procedure calls. The main program coordinates calls to procedures and hands over appropriate data as parameters. The data is processed by the procedures and, once the program has finished, the resulting data is presented. Thus, the flow of data can be illustrated as a hierarchical graph, a tree for a program with no subprocedures.

 

A single program that is divided into small pieces called procedures. To enable usage of general procedures or groups of procedures also in other programs, they must be separately available. For that reason, modular programming allows grouping of procedures into modules.

Modular Programming

With modular programming procedures of a common functionality are grouped together into separate modules. A program therefore no longer consists of only one single part. It is now divided into several smaller parts which interact through procedure calls and which form the whole program. The main program coordinates calls to procedures in separate modules and hands over appropriate data as parameters.

 

Each module can have its own data. This allows each module to manage an internal state which is modified by calls to procedures of this module. However, there is only one state per module and each module exists at most once in the whole program.

Long programs are written in small sections or modules. The advantages of this approach are:

  • Small sections are easier to debug
  • Once a module is thoroughly tested, it can be incorporated into the larger program with confidence
  • Modules can be re-used in different programs
  • Different programmers can work on different modules
  • Programmers can specialize in certain techniques
  • Modular code is easier to read, and hence to update

Modules come in various guises. They used to be called sub-routines. The common module types are procedures, functions and objects.

Note: Pascal is used as the sample language in the examples; other languages have similar statements

Variables

Data handled by a program is stored in named sections of memory called variables. This data can be changed as the program progresses. It is easier to refer to names than to the memory addresses where the data is stored. In a high-level language a variable will be associated with a data type that will specify the type of data that can be stored. Examples are

        number : integer;

        amount : real;

        status : Boolean;

        answer : character;

        

Constants

Any value that is required to be the same all the way through a program can be set up as a named constant. Reference can be made without the need to enter its value at many places. An example might be:

        Pie = 3.1423

Variables and constants, and other named components of a program, are given their names by the programmer. The names are called identifiers. It is important that these identifiers mean something, so that as the program develops it remains easy to understand.

Assignment Statement:

A value is computed and then assigned to a variable in the memory of the computer. An example might be:

x := y + z

m := 30

These statements are called assignment statements because they assign a value to a  location in memory. In the examples above, the values in memory locations Y and Z are added together and the result placed in X, and the value 30 is placed in M.

Comments

All high level languages have the facility to insert comments in the program code. These are brief messages that help to remind the programmer what a particular line or section means when returning to it later. A special symbol is used to tell the compiler to ignore the comment line. Comments are not translated, so take up no space in the final compiled program. Comments are also useful in tracking down errors in a program. Lines can be ‘commented out’ in order to see if they are the cause of the problem.

Selection Statement

When the program has to make a decision as to the next operation to be carried out a selection statement is used. The simplest selection statement is the IF statement. An example is:

        IF amount > creditlimit THEN

            Write(‘authorisation required’)

           ELSE

                Write(‘authorisation not required’)

           END

This statement will cause the program to output ‘Authorisation required’ if the value of the variable amount is greater than the value of the variable creditlimit. If the value of amount is not greater than creditlimit then the output will be ‘Authorisation not required’

The conditions that can be checked are known as Boolean conditions. The conditional operators are <, >, >=, <=, <> , =.

A more complex selection statement is the case statement (sometimes called a switch statement). This statement causes the computer to select one of a list of options, for example:

        Case Menuchoice of

                ‘A’ : writeln(‘you have chosen Add’)

                ‘C’ : writeln(‘You have chosen change’)

                ‘D’ : writeln(‘You have chosen Delete’)

        end;

Case statement could be achieved with If…Then….Else statements but the Case statement is a neater form of selection statement when there are more than two possible outcomes to be tested

Iterations

An iteration statement allows the execution of a series of statements a number of times(often called a loop). There are two main types of loop:

  • While/Repeat – used when the number of loops to be taken is unknown on entry (conditional loop)
  • For – used when the number of loops is known in advance (unconditional loop)

The While loop checks a Boolean condition before entering the loop, so that the loop is not performed at all if the condition is false. For example,

         Total = 0

        Readln(mark)

        While mark <> - 1 do

        Begin

                Total = total + mark

                Writeln(‘please enter the next mark , -1 to end’)

                Readln(mark)

        End

        Writeln(‘Total of all marks is ’, total)

If the mark read in at the second line is equal to –1, the statements in the loop will not be executed at all and the next line to be executed will be

        Writeln(‘Total of all marks is ’, total)

The repeat..until loop checks the Boolean condition at the end of the loop and therefore the loop is always performed at least once. For example,

Join now!

        Total = 0

        Readln(mark)

        repeat

                Total = total + mark

                Writeln(‘please enter the next mark , -1 to end’)

                Readln(mark)

        Until mark = -1

        Writeln(‘Total of all marks is ’, total)

Difference between these two types of loops:

  1. In a While loop, the Boolean condition is checked before entering the loop whereas in a repeat..until loop, it is checked at the end of the loop
  2. The while loop iterates on a true Boolean condition whereas in a repeat..until, it iterates on a false boolean condition
  3. The repeat..until loop will execute at least once whereas the while loop ...

This is a preview of the whole essay