Computer Programming Notes-2
Computer Programming Notes-2
DAVID
Definition of terms
Computer Program:
A computer program is a set of coded instructions given to the computer, and represents a logical solution
to a problem. It directs a computer in performing various operations/tasks on the data supplied to it.
Computer programs may be written by the hardware manufacturers, Software houses, or a programmer to
solve user problems on the computer.
Programming:
Programming is the process of designing a set of instructions (computer programs) which can be used to
perform a particular task or solve a specific problem.
It involves use of special characters, signs and symbols found in a particular programming language to
create computer instructions.
A computer program performs the following:
1. Accepts data from outside the computer as its input.
2. Carries out a set of processes on the data within the computer memory.
3. Presents the results of this processing as its output, and
4. Stores the data for future use.
Programming Languages:
A programming language is a set of symbols (a language) which a computer programmer uses to solve a
given problem using a computer.
The computer must be able to translate these instructions into machine-readable form when arranged in a
particular sequence or order
TERMS USED IN COMPUTER PROGRAMMING
Source program (source code)
The term Source program refers to program statements that the programmer enters in the program editor
window, and which have not yet been translated into machine-readable form.
Source code is the code understood by the programmer, and is usually written in high-level language or
Assembly language.
Object code (object program).
The term Object code refers to the program code that is in machine-readable (binary) form.
This is the code/language the computer can understand, and is produced by a Compiler or Assembler after
translating the Source program into a form that can be readily loaded into the computer.
LANGUAGE TRANSLATORS
A computer uses & stores information in binary form, and therefore, it cannot understand programs
written in either high-level or low-level languages. This means that, any program code written in
Assembly language or high-level language must be translated into Machine language, before the
computer can recognize & run these programs.
A Translator is special system software used to convert the Source codes (program statements written in
any of the computer programming languages) to their Object codes (computer language equivalents).
The Translators reside in the main memory of the computer, and use the program code of the high-level or
Assembly language as input data, changes the codes, and gives the output program in machine-readable
code.
Each language needs its own translator. Generally, there are 3 types of language translators:
1. Assembler.
2. Interpreter.
3. Compiler.
Note. Interpreters & Compilers translate source programs written in high-level languages to their machine
language equivalents.
Assembler
An assembler translates programs written in Assembly language into machine language that the computer
can understand and execute.
Functions of an Assembler.
1). It checks whether the instructions written are valid, and identifies any errors in the program.
The Assembler will display these errors as well as the complete source and object programs. If the
program has no errors, the job control will let it run immediately, or save the object program so that
it may run it later without translating it again.
2). It assigns memory locations to the names the programmer uses.
1
COMPUTER PROGRAMMING MR.DAVID
E.g., the Assembler keeps a table of these names so that if an instruction refers to it, the Assembler
can easily tell the location to which it was assigned.
3). It generates the machine code equivalent of the Assembly instructions.
Usually, the Assembler generates a machine code only when no errors are detected. Some of the
errors include;
- Typing mistakes.
- Using the wrong format for an instruction.
- Specifying a memory location outside the range 0 – 2047.
Note. The Assembler cannot detect Logic errors. The programmer knows of these errors only
when the program is run & the results produced are incorrect (not what the programmer expected).
The programmer must therefore, go through the program & try to discover why an incorrect result
was being produced.
Interpreter
An interpreter translates a source program word by word or line by line. This allows the CPU to execute
one line at a time.
The Interpreter takes one line of the source program, translates it into a machine instruction, and then it is
immediately executed by the CPU. It then takes the next instruction, translates it into a machine
instruction, and then the CPU executes it, and so on.
The translated line is not stored in the computer memory. Therefore, every time the program is needed
for execution, it has to be translated.
Compiler
A compiler translates the entire/whole source program into object code at once, and then executes it in
machine language code. These machine code instructions can then be run on the computer to perform the
particular task as specified in the high-level program.
The process of translating a program written in a high-level source language into machine language using
a compiler is called Compilation
Points to note.
The job of a Compiler is much more difficult than that of an Assembler in that, a single statement in a
high-level language is equivalent to many machine instructions.
The format of an Assembly instruction is fairly fixed, while high-level languages give a lot of freedom
in the way the programmer writes statements.
Functions of a compiler.
A Compiler performs the following tasks during the compilation process:
1). It identifies the proper order of processing, so as to execute the process as fast as possible &
minimize the storage space required in memory.
2). It allocates space in memory for the storage locations defined in the program to be executed.
3). It reads each line of the source program & converts it into machine language.
4). It checks for Syntax errors in a program (i.e., statements which do not conform to the grammatical
rules of the language). If there are no syntax errors, it generates machine code equivalent to the
given program.
5). It combines the program (machine) code generated with the appropriate subroutines from the
library.
6). It produces a listing of the program, indicating errors, if any.
2. Translates the program each time it is needed 2. Compiled programs (object codes) can be
for execution; hence, it is slower than saved on a storage media and run when
compiling. required; hence executes faster than
2
COMPUTER PROGRAMMING MR.DAVID
interpreted programs.
3. Interpreted object codes take less memory
compared to compiled programs. 3. Compiled programs require more memory as
their object files are larger.
4. For an Interpreter, the syntax (grammatical)
errors are reported & corrected before the 4. For a Compiler, the syntax errors are reported
execution can continue. & corrected after the source code has been
translated to its object code equivalent.
5. An Interpreter can relate error messages to the
source program, which is always available to 5. Once the source program has been translated,
the Interpreter. This makes debugging of a it is no longer available to the Compiler, so
program easier when using an Interpreter than the error messages are usually less
a Compiler. meaningful.
These are the basic programming languages, which can easily be understood by the computer directly, or
which require little effort to be translated into computer understandable form.
They include:
1. Machine languages.
2. Assembly languages.
Features of low-level languages
They are machine hardware-oriented.
They are not portable, i.e., a program written for one computer cannot be installed and used on another
computer of a different family.
They use Mnemonic codes.
They frequently use symbolic addresses.
Machine languages (1st Generation languages)
Machine language is written using machine codes (binary digits) that consist of 0’s & 1’s.
The computer can readily understand Machine code (language) instructions without any translation.
A programmer is required to write his program in strings of 0’s & 1’s, calculate & allocate the core
memory locations for his data and/or instructions.
Different CPU’s have different machine codes, e.g., codes written for the Intel Pentium processors may
differ from those written for Motorola or Cyrix processors. Therefore, before interpreting the meaning of
a particular code, a programmer must know for which CPU the program was written.
3
COMPUTER PROGRAMMING MR.DAVID
4
COMPUTER PROGRAMMING MR.DAVID
Programs written in a high-level language cannot be obeyed by the computer hardware directly.
Therefore, the source codes must be translated into their corresponding machine language equivalent. The
translation process is carried out by a high-level language software translator such as a Compiler or an
Interpreter.
Features of high-level programming languages.
They contain statements that have an extensive vocabulary of words, symbols, sentences & mathematical
expressions, which are very similar to the normal English language
Allow modularization (sub-routines).
They are ‘user-friendly’ and problem-oriented rather than machine-based. This implies that, during a
programming session, the programmer concentrates on problem-solving rather than how a machine
operates.
They require one to be obey a set of rules when writing the program.
Programs written in high-level languages are shorter than their low-level language equivalents, since
one statement translates into several machine code instructions.
The programs are portable between different computers.
Purpose of High-level languages.
1. To improve the productivity of a programmer. This is because; the source programs of high-level
languages are shorter than the source programs of low-level languages, since one statement translates
into several machine code instructions.
2. To ease the training of new programmers, since there is no need to learn the detailed layout of a
procession/sequence.
3. To speed up testing & error correction.
4. To make programs easy to understand & follow.
Advantages of High-level languages.
1. They are easily portable, i.e., they can be transferred between computers of different families and run
with little or no modification.
2. High-level language programs are short, and take shorter time to be translated.
3. They are easy to lean, understand and use.
4. They are easy to debug (correct/remove errors), & maintain.
5. High level language programs are easy to modify, and also to incorporate additional features thus
enhancing its functional capabilities.
6. They are ‘user-friendly’ & problem-oriented; hence, can be used to solve problems arising from the
real world.
7. They enable programmers to adapt easily to new hardware. This is because; they don’t have to worry
about the hardware design of the computer.
8. High-level language programs are self-documenting, i.e., the program statements displays the
transparency of purpose making the verification of the program easy.
9. High level languages are more flexible; hence, they enhance the creativity of the programmer and
increase his/her productivity in the workplace.
Disadvantages of using High-level languages
1. High-level languages are not machine-oriented; hence, they do not use of the CPU and hardware
facilities efficiently.
2. The languages are machine-independent, and cannot be used in programming the hardware directly.
3. Each high-level language statement converts into several machine code instructions. This means that,
5
COMPUTER PROGRAMMING MR.DAVID
they use more storage space, and it also takes more time to run the program.
4. Their program statements are too general; hence, they execute slowly than their machine code
program equivalents.
5. They have to be interpreted or compiled to machine-readable form before the computer can execute
them.
6. The languages cannot be used on very small computers.
The source program written in a high-level language needs a Compiler, which is loaded into the main
memory of the computer, and thus occupies much of memory space. This greatly reduces the
memory available for a source program.
TYPES OF HIGH-LEVEL LANGUAGES.
High-level languages are classified into five different groups:
1. Third generation languages (Structured / Procedural languages).
2. Fourth generation languages (4GLs).
3. Fifth generation languages (5GLs)
4. Object-oriented programming languages (OOPs).
5. Web scripting languages.
STRUCTURED LANGUAGES
A structured (procedural) language allows a large program to be broken into smaller sub-programs called
modules, each performing a particular (single) task. This technique of program design is referred to as
structured programming.
Structured programming also makes use of a few simple control structures in problem solving. The 3
basic control structures are:
Sequence
Selection.
Iteration (looping).
Advantages of structured programming.
1. It is flexible.
2. Structured programs are easier to read.
3. Programs are easy to modify because; a programmer can change the details of a section without
affecting the rest of the program.
4. It is easier to document specific tasks.
5. Use of modules that contain standard procedures throughout the program saves development time.
6. Modules can be named in such a way that, they are consistent and easy to find in documentation.
7. Debugging is easier because; each module can be designed, coded & tested independently.
Examples of Third generation programming languages include
1). BASIC (Beginners All-purpose Symbolic Instructional Code).
2). PASCAL-PASCAL is a general-purpose, high-level programming language, which was named after
a French mathematician called Blaise Pascal.
3). COBOL (COmmon Business Oriented Language)-COBOL is designed for developing programs
that solve business problems
4). FORTRAN (FORmula TRANslator)-It was developed for mathematicians, scientists and engineers
5). Ada-This language was named after the first lady programmer Ada Lovelace.
It is suitable for developing military, industrial and real-time systems.
6). C- is mainly used for developing system software such as the operating system as well as developing
the application packages
7). LOGO- was designed for educational use in which children can explore & develop concepts through
programming the movement of a pen.
8). COROL-COROL is used in Real-time processing.
COROL programs are compiled.
9). RPG (Report Program Generator)-RPG is used in report generating applications, (i.e. it is designed
to facilitate the output of reports of business data).
A Report generator is a software tool that extracts stored data to create customized reports that are
not normally/usually produced by existing applications.
6
COMPUTER PROGRAMMING MR.DAVID
10). SNOBOL (String Oriented Symbolic Language).-It is a high-level language designed to manipulate
strings of characters. It is therefore used for non-numeric applications
FOURTH GENERATION LANGUAGES (4GL’S).
4GLs make programming even easier than the 3GLs because; they present the programmer with
more programming tools, such as command buttons, forms, textboxes etc. The programmer simply
selects graphical objects called controls on the screen, and then uses them to create designs on a
form by dragging a mouse pointer
4GLs are used to enquire & access the data stored in database systems; hence, they are described as the
Query languages.
Purpose of fourth generation languages.
7
COMPUTER PROGRAMMING MR.DAVID
8
COMPUTER PROGRAMMING MR.DAVID
9
COMPUTER PROGRAMMING MR.DAVID
Generation Technology
First generation A). Very Large Integrated Circuit
Second B). Thermionic valves (Vacuum tubes)
generation
Third generation C). Transistors
Fourth generation D). Integrated Circuits
1
COMPUTER PROGRAMMING MR.DAVID
1
1