Computer Science Principles V11

Clive W. Humphris

COMPUTER PROGRAMMING: Programming Structure.  


A computer program is a list of instructions or commands which the computer hardware follows in order to execute a defined task. In this simple example to show a program structure, a user inputs two numbers, the program adds them together and prints the result (assume all inputs are made from the keyboard and all outputs are to the computer screen).

The person who writes the list of instructions is called a programmer and the set of rules used is called the programming language. The computer program follows the list of instructions and electronically changes the various logic states in the computer hardware to produce the desired output.

On the left of the diagram is the flow diagram which is a graphical means of designing the computer program using standard symbols. This is then coded into the computer program by the programmer as a series of instructions, which are followed sequentially from start to finish when the user types the command 'RUN'. In its simplest form (without the program loop) the program asks for two inputs, adds them and prints the results.

Taking a more practical approach a program loop is included which forces the program to return to the beginning to enable a further pair numbers to be input. A problem here is that this sequence of events must continue forever as there is no way of breaking out of the program loop, also both inputs have to be repeated each time, even if one or both never change.

Dimensioned variables.

Computer memory locations (RAM) have to be allocated to each of the variables used in the program to store the values that each is given. The term used is called 'dimensioning'. Depending upon the type of variable i.e. number, string of characters. Then a number of contiguous memory locations are reserved under that variable name, for example, one for FIRST_NUMBER, another for SECOND_NUMBER and another for the variable named RESULT.

With high level languages the physical location of these variables in the computer memory is of no concern to the programmer as the operating system carries out this function. In fact during the execution of the program the operating system will move variables around. As space is dynamically allocated for other variables and operations.

Operators used.


An operator is a arithmetic or logic symbol to instruct the computer to carry out the specified function. In this case add two numbers together.

Add the contents of the variable FIRST_NUMBER before the '+' operator and the contents of the following variable SECOND_NUMBER. The result of this operation will be temporarily stored in the hardware 'accumulator' register which always holds the result of the last arithmetic or logic operation.

REM To input two values, add and output result.

REM is short for 'REMARK' and is used to include explanatory comments in program code. When the program is executed and it comes across a REM statement it will ignore the remainder of that line. Comments are included for the benefit of the programmer or more importantly other programmers looking at the code months or years later and trying to make some sense of how the program works.

It's easy to get carried away with the enthusiasm of programming and not bother to document code properly.

Whilst it's true that high level languages are self explanatory by the choice of variable names which self document their purpose or content. Later when it comes to modifying that code or debugging, especially by somebody else then the use of REM statements becomes invaluable. The generous use of comments does not effect the run-time of a program and they are ignored when the final code is compiled.

INPUT FIRST_NUMBER


INPUT is a command recognised by the computer to read the keyboard input buffer for an electronic representation of the user key-press.

Program variables are names which represent computer memory store locations. Think of the memory as a series of mailboxes at the postal sorting office, each one is allocated an address, which is a person's home or workplace. Post for that person is placed in the box. Here we need three locations one called FIRST_NUMBER, another named SECOND_NUMBER and a third for RESULT. Except with computers we electronically hold the value of the item in the post-box (memory address).

Depending upon the variable type then one or more memory locations need to be allocated at the beginning of the program. As our numbers are all likely to be low values, say less than 255 then one eight bit location will be sufficient. Numbers greater than this will require two (16bits) and for strings of characters possibly thousands. Just think of the text in a word processor document.

LET RESULT = FIRST_NUMBER + SECOND_NUMBER


The LET statement assigns a value to an expression. In modern languages 'LET' is ignored and a line of code such as LET C = A + B is shortened to C = A + B.

The LET word or when its inferred means carry out the instruction by adding the contents of A, and B and placing the results in memory location C. You will see that we were using variable names like FIRST_NUMBER we are now using A, B and C. All are valid variable names, but using another name means a new memory location is allocated. You can if you wish, write a line of code where RESULT = A + B. but you also need to change the input program lines.

PRINT RESULT


PRINT is a command which is interpreted by the computer to mean print text or the contents of a specified memory locations to the screen or printer or other output device. Further programming instructions would need to be included to specify the font, output device location and colour of the printed output.

Within the programming language the PRINT command can include other formatting characters for spacing and tabulations, thereby enabling data to be professionally laid out on the output display. However, with the use of object oriented languages where output is directed to labels, textboxes, tables etc, the simple Print command is replaced by other output methods.

RUN


Once a computer program has been coded (called design time) from the flow diagram there comes a time when it must be tested to see if it performs as required. This is now run-time and started by the RUN command.

The computer operating system looks at the first line of code for an instruction. In this case the first line is ignored as it's a REM statement and moves to the following line. Next it reads the keyboard for the first and second numbers. Adds them together and prints out the result to the computer screen. Depending upon the following instruction the program ends or loops back to the beginning.

END

END is always the last line of the program code. It stops running the program and no further instructions are carried out.

GOTO START

GOTO is a an instruction which causes the program to branch unconditionally to a specified line within the program. Usually the branch location is given a label or line number.

Computer programming using GOTO statements is not considered good practice as there are much better ways of incorporating branches. One of the problems of using GOTOs is debugging program code, as they can give unpredictable results when code loops become intertwined. However, GOTOs are frequently used to force program jumps when program errors are encountered.

Start

Symbol used to begin or end a series of steps drawn as a flow diagram.

The flow diagram represents an algorithm which is a series of program steps, when followed will provide a solution to a specific problem.

Input First No.

Input symbols represent a specified self contained computer program function. To read the keyboard, and store the value in a specified memory location.

At this stage we are not concerned with variable names, all we need to show is what to do and how it fits in with the other parts of the program.

Add Numbers Store Result

The symbol shows that the adding of two numbers is to be a computer operation. Within the program code the variable names will be allocated and the arithmetic operator '+' applied where the result is stored in another (at present) unspecified location.

Using the flow diagram to design software enables the program to be structured into self-contained manageable sections which can be fully tested to see that they meet the required objectives. Several if not tens of programmers will often share very large computer projects. Using a flowchart each programmer can be allocated a specific task and clearly see how it fits in with other operations.

Print Result

The output flowchart symbol is the same as for an input. As a self-contained function it reads the contents of the specified memory location called RESULT and prints it to the screen.

End

Flowchart symbol to signify the program END. Normally for simple programming tasks there is only one END symbol.

Table of contents

previous page start next page