Introduction to Computer Programming
Unit 9 INTRODUCTION TO COMPUTER
PROGRAMMING
Key Unit Competency
By the end of the unit, you should be able to explain programming paradigms.
Unit Outline
• Computer programming concepts.
• History of programming languages.
• Highlevel programming languages.
• Computer programming paradigm.
• Features of good programming language.
Introduction
Computers have been applied in different areas, from controlling nuclear plants to
providing games in mobile phones. Because of this diversity in computer use, a
computer, tablet or mobile must have relevant programs. This unit introduces basic
concepts used in computer programming, evolution of programming languages and
programming paradigms since the advent of the first programmable machine.
Activity 9.1: Computer programming concepts
The structure of any language such as Kinyarwanda, Kiswahili, French, English or
Chinese is described in terms of form (syntax) and meaning (semantic). In groups,
research on the internet and use your knowledge in language studies to brainstorm
on the two concepts.
9.1 Computer Programming Concepts
Before we begin discussing the details of computer programming, we need to consider
a few concepts that will be used from time to time in the rest of this book. In this
section, we briefly highlight some of the fundamental concepts used in programming
which includes:
9.1.1 Computer program
A computer program refer to a set of instructions, written using a programming
language to instruct a computer to perform a specified task. A program is like a recipe.
It contains a list of ingredients (referred to as variables) and a list of instruction
(statements) that tell the computer what to do with the variables.
9.1.2 Software
Though the term software and program are used interchangeably, technically, software
refers to a program and associated documentations, while a program is basically a
set of executable instructions loadable into computer memory.
171
Introduction to Computer Programming
9.1.3 Programming
Computer programming is a systematic process of writing a computer program using
programming languages. The person who writes computer programs is referred to as
a programmer. Other terms used to refer to a programmer are software developer
and software engineer.
9.1.4 Programming languages
A programming language is a formal language that specifies syntax and semantics
rules used in writing a computer program. Some examples of programming languages
include BASIC, C, C++, Java, Pascal, FORTRAN and COBOL.
9.1.5 Source code
The term source code refers to a set of instructions or statements written by a
programmer that are not yet translated into machine-readable form. A source code is
mostly a text file written using programming languages like BASIC, Pascal, C or C++.
9.1.6 Object code
Once a source code is written, it can be translated into machine readable form referred
to as object code. To translate source code statement to object code is similar to the
way one can translate English to Kinyarwanda, there are language translators used
to translate source code to object code.
9.1.7 Compilers and interpreters
A compiler is a language process that translates the entire source code into object
code. The object file can be made into an executable program by carrying out another
process known as linking. Linking combines compiled code with one or more existing
object codes to create an execution file. In Windows operating system, you can easily
identify an executable file because it has an EXE extension such as winword.exe.
Unlike a compiler that translate the entire source code to object code, an interpreter
translates source code one statement at a time. Because the interpreted statements
are saved as an executable file, every time the program is run, each statement must
be interpreted. Table 9.1 gives a summary of differences between compilers and
interpreters.
Interpreters Compilers
Translates source code one statement at a Translates the entire source code at once before
time. execution.
Translates the program each time it is run hence Compiled object code is saved on the disk hence
slower than compiling. runs faster than interpreted programs.
Interpreted object code takes less memory Compiled programs require more storage to
compared to compiled program. store the object.
Table 9.1: Difference between compilers and interpreters.
172
Introduction to Computer Programming
Activity 9.2: Computer programming
Most students wonder how they would benefit from the study of mathematics and
computer programming. Brainstorm 5 benefits of learning both mathematics and
computer programming in your studies.
Assessment Exercise 9.1
1. Define the terms:
(a) computer programming,
(b) source code.
(c) object code.
2. Differentiate between the compilers and interpreters.
3. Though the terms program and software are used interchangeably, they are
technically different. Explain the difference between the two.
9.2 History of Programming languages
The person to be credited as the first programmer was a lady by the name Ada Byron
in early 1800. Since then many programming languages have been developed over the
years. These languages can be classified into two main categories and five generations.
The first and second generations consist of low-level languages while the third to
fifth generations comprise of high-level languages.
9.2.1 Low-level Programming Languages
Low-level languages are regarded as low because they can be directly understood
by a computer while some requires minimal translation to machine readable form.
Low level-languages are classified into two generations: first generation languages
also known as machine languages, and second generation languages referred to as
assembly languages.
9.2.2 First Generation Languages
First generation languages (1-GLs) refers to machine languages (binary code) used to
program the first generation programmable computers such as UNIVAC and ENIAC.
These computers were programmed by connecting wires on plug boards. The wiring
configuration was used to represent data in binary form as a series of on’s (1) and
off’s (0) in electronic circuits. Fig. 9.1 shows a sample binary code representing a
program used to operate machines such as ENIAC.
173
Introduction to Computer Programming
11100011 00000001 10000011
00011100 10001101 10001101
10001111 11111000 10000001
Fig.9.1: Machine (binary) code
NB: Machine programming was very slow, tedious and error prone. Furthermore,
such a program is not portable because first electronic computers deferred from one
another.
9.2.3 Second Generation Languages
The second generation languages (2-GLs) referred to as assembly languages marked
the first successful attempt to make programming easier and faster. Most assembly
languages allowed programmers to write programs as a set of symbolic codes known
as mnemonics. Mnemonics are basically an abbreviation of keywords as shown in
Fig. 9.2.
mov ax, [40005] 1: move content from address 40005 to register
ax.
add ax, 45 2:add 45 to content in ax.
jp 11300 3: if the sum is greater than 0, jump to location
11300
Fig.9.2: Assembly program code
Unlike machine languages, program code written in assembly language has to be
translated to machine code using a language processor known as assembler. An
assembler is a special program that converts instructions written in low-level assembly
code into machine code. Nevertheless, programs written using assembly languages
are machine dependent hence not portable.
Activity 9.3: Second generation programming languages
Research on the internet the programming languages used on Second generation
computers such as IBM7094 and UNIVAC 1108.
9.2.4 Benefits and limitations of low-level languages
Having looked at the two categories of low-level programming languages, let’s
highlight some of the benefits and limitations of low-level languages.
Benefits
1. Program written using low level languages requires small amount of memory
space.
2. The processor executes them faster because they require minimal or no
translation.
3. Low level languages are stable and hardly crash or break down once written.
174
Introduction to Computer Programming
Limitations
1. Low level languages are difficult and cumbersome to use and learn.
2. They require highly trained experts both to develop and maintain.
3. Checking for errors (debugging) in low level programs is difficult and time
consuming.
4. Low level programs are machine dependent hence they are not portable.
Assessment Exercise 9.2
1. Define the terms binary code, mnemonics, and assembler.
2. Differentiate between machine languages and assembly languages.
3. Explain how the first generation computers were programmed using binary code.
4. Highlight three advantages and three disadvantages of low level languages.
5. Mr. Kwizera bought a new electrical kettle. On the power switch it was inscribed
digits 0 and 1:
(a) Explain what each of the two symbols stand for.
(b) Explain why the two symbols are important in computers and computer
programming.
9.3 High-level Programming Languages
Due to drawbacks of low-level languages, high-level languages began to appear in
1950’s. High level languages that closely resembles natural (human) languages like
English. Unlike low-level languages, high-level languages are independent of machine
architecture. This means that, instead of a programmer spending more time learning
the architecture of the underlying machine, more time is devoted towards solving
a computing problem. Generally, high-level programming languages are classified
into three generations namely: third generation (3-GLs), fourth generation (4-GLs),
and fifth generation (5-GLs) programming languages.
9.3.1 Third generation languages
Third level languages (3-GLs) are also known as procedural or structured programming
languages. Procedural languages make it possible to break down a program into
components known as procedures or modules each performing a particular task.
Examples of 3-GL include Pascal, FORTRAN (Formula Translator), BASIC
(Beginners All-Purpose Symbolic Instruction Code), C, C++, Adca and COBOL
(Common Business Oriented Language).
9.3.2 Fourth generation languages
Fourth generation languages (4-GLs) were improvement on 3GLs meant to reduce
programming effort by making programming more easier and flexible.
175
Introduction to Computer Programming
Furthermore, most 4GLs incorporates advanced programming tools for integrating
programs with databases and generating summarised reports. Examples of 4-GLs
include Structured Query Language (SQL), Focus, PostScript, RPG II,
PowerBuilder, FoxPro, Python, Progress 4GL, and Visual Basic.
9.3.3 Fifth generation languages
Fifth generation languages (5-GLs) also known as natural languages are used to
develop systems that solve problems using artificial intelligence. Artificial intelligence
refers to computer systems that mimic human-like intelligence. Such intelligence
include visual (seeing), perception, speech recognition, decision making and
movement. Therefore, in 5GL programming, the programmer only worries about
constraints required for the problem to be solved. Typical examples of 5GLs include
Prolog, LISP, Scheme, Ocaml, and Mercury.
9.3.4 Benefits and limitations of high-level languages
Having looked at the various high-level programming languages, let’s highlight some
of the benefits and limitations associated with most of these languages.
9.3.4.1 Benefits
1. High level languages are portable i.e. they are transferable from one computer
to another.
2. High level languages are user friendly and easy to use and learn.
3. High level languages are more flexible, hence they enhance the creativity of the
programmer and increase productivity in the workplace.
4. A program in high level languages is easier to correct errors.
9.3.4.2 Limitations
1. Their nature encourages use of many instructions in a word or statement hence
the complexity of these instructions slows down program processing.
2. They have to be interpreted or compiled to machine readable form before the
computer can execute them.
3. They require large computer memory to run.
Assessment Exercise 9.3
1. Distinguish between the following terms:
(a) Third generation.
(b) Fourth generation programming languages.
2. Briefly explain the evolution of programming languages. In each case, identify
the generation and languages used.
3. State three advantages and three disadvantages of high-level languages.
4. Identify and discuss five examples of structured programming language.
176
Introduction to Computer Programming
9.4 Computer Programming Paradigms
The term paradigm was first used by Thomas Kuhn in his 1962 to refer to theoretical
frameworks within which all scientific thinking and practices operate. In other words,
paradigm refers to theory or ideas concerning how something should be done, made,
or thought about. Paradigm shift refers to fundamental change on how something
should be done, made, or thought about.
9.4.1 Definition of Programming Paradigm
Programming paradigm refers to pattern, theory or systems of ideas that are used to
guide development of computer programs. In other words, it is a school of thought or
philosophy that defines concepts, practices and views on how computer programming
should be conceptualized or performed. Several programming paradigms have
evolved each of which presents programmers with a specific mode of thinking about
computer programming. In the next section, we classify programming paradigms into
imperative, functional, logic and object oriented.
9.4.2 Classification of Programming Paradigms
Programming paradigm may be classified into four main categories namely imperative
programming, functional programming, logic programming and object-oriented
programming.
9.4.2.1 Imperative programming paradigm
Imperative programming also referred to as procedure-oriented is a paradigm in
which commands (program instructions) are executed in sequential order. One of the
fundamental characteristic of programs written using imperative languages is that
they have variables that change during program execution. For example, consider
the following statement that adds two numbers x and y and assigns the result to a
variable named sum:
sum = x + y
Every time different values for variables x and y are provided, sum changes from the
previous state to new state as shown in Table 9.2.
x y Sum = x + y Remarks
8 9 17 17 is current state
10 12 22 17 replaced by 22
15 30 45 22 replaced by 45
Table 9.2: New state of variables
Programming languages that support imperative programming including machine
languages, assembly languages, Basic, Pascal and C.
177
Introduction to Computer Programming
9.4.2.2 Functional Programming Paradigm
Functional programming is a paradigm based on concept of functions that consists
of the function name and list of values known as parameters enclosed in parenthesis.
The main difference between functional programming and imperative paradigm is that
functional programming does not require use of assignment statements to manipulate
variables. Instead, manipulation of variables is accomplished by applying functions
to a list of parameters also known as arguments. The following syntax known as
polish notation is used to represent a function and list of arguments:
(function_name parameter1... parametern);
For example, consider a function that calculates sum of four parameters 5, 4, 7 and
9. We can use addition symbol (+) or mnemonic add to represent addition function
as follows:
(+ 5 4 7 9) or (Add 5 4 7 9)
In this case, the function takes four parameters to calculate the total; this gives us
25. The parameters in this example can also be manipulated using other arithmetic
functions like subtraction (-), multiplication (*) and division (/). Examples of
programming languages that support functional paradigm include LISP, Scheme,
Haskell, MetaLanguage (ML), Miranda, Caml, and F#.
Activity 9.4: Programming paradigms
1) Using examples, differentiate between imperative, and functional programming
paradigms.
2) Brainstorm on benefits and limitations of functional programming paradigm.
3) Using polish notation write a function that can subtract and multiply three
parameters.
9.4.2.3 Logic Programming Paradigm
Logic programming is a rule-based paradigm that focuses on use of logic or predicate
calculus. In logic programming paradigms, only facts and rules are declared to produce
desired results. This means that a logic program is a set of facts that make use of a
set of rules to answer a query. For example, the following statement in a language
known as Prolog (standards for programming logic) could mean that if ann is the
mother of shella, then ann is an ancestor of shella:
ancestor(ann, shella) :- mother(ann, shella).
Logic programming paradigm fits well when applied in artificial intelligence (AI)
that deal with the extraction of knowledge from basic facts and rules. In artificial
intelligence, various logical assertions (proportions) about a situation are made to
establish all known facts. Languages that emphasize logic programming paradigm
include Prolog, GHC, Parlog, Vulcan, Polka and Mercury.
178
Introduction to Computer Programming
Activity 9.5: Logic programming
• Brainstorm on benefits and limitations of logic programming paradigm.
• Use a sample functional program to demonstrate how rule-based program
statements are executed in regard to facts, rules, inference and answers to queries.
9.4.2.4 Object Oriented Programming Paradigm
Object-Oriented Programming Paradigm (OOP) is the latest paradigm in which
properties (data) and operations (procedures) are combined to form objects.
Therefore, an object represents a real-world “thing” such as a person, animal, plant,
place, or building. In object-oriented programming, similar objects are grouped
together to form classes. For example, the Table 9.3 below shows three types of
classes that define properties and operations applicable to each object:
Class Properties (data) Sample object Operation
Person first name, surname, gender “Peter, Muse, Male” Add, delete, edit, person
Building House No, Type, Town “H34, Bungalow, Add, delete, edit, building
Kigali”
Plants Type, Name, Height “Tree, cypress, 5 Add, delete, edit, plant
metre”
Table 9.3: Classes and objects
Because the latest paradigm shift is development of OPP programs, most imperative
languages like C, Pascal and Basic have evolved to support OOP. Examples of
programming languages that support OOP include Delphi Pascal, C++, Java, C#,
Visual Basic.Net, and Objective-C.
In summary, Table 9.4 shows the four major programming paradigms namely
imperative, functional, logic, and object-oriented programming:
Program
Paradigm Concept Description Program Results
execution
Imperative Commands Computations as Sequence of Executions of Final state
(instructions) statements that commands commands of computer
directly change a memory
program state
Functional Function Treats Collection of Evaluation of Value of
computation as functions function the main
the evaluation function
of mathematical
functions
avoiding change
of state
Table 9.4: Summary of programming paradigms languages (continued next page)
179
Introduction to Computer Programming
Program
Paradigm Concept Description Program Results
execution
Logic Predicate Treats a program Logic formulas: Logic proving of Failure or
as a set of axioms and theorem theorem success of
propositions proving
comprising of
rules and facts
Object- Objects and Treats a program Collection of Exchange Final state
oriented classes as a collection objects of messages of objects
of objects that between objects
have state and
behaviour
Table 9.4: Summary of programming paradigms languages
Activity 9.6: OOP Paradigm
1. Some procedural programming languages support the object oriented paradigm.
Differentiate the object ariented paradigm and procedural paradigm.
2. Discuss the terms classes, inheritance and polymorphism.
3. What are the benefits and limitations of object-oriented programming ?
9.5 Features of Good Programming Language
Criteria for evaluating programming languages and paradigms may be controversial
but Sebesta in his book, “Concepts of Programming Languages, tenth edition”
suggests four main criteria namely: readability, writability, reliability and cost.
• Overall simplicity: Overall simplicity of a programming language influences its
ease of learning and readability.
• Good orthogonality: Relatively small set of simple constructs can be combined in
a number of ways to provide required control and data structures of the language.
Limited orthogonalilty makes it easier to learn, read, and understand a language.
• Adequate data types and data structures: Presence of adequate facilities
for defining data types and data structures help increase the readability of a
programming language.
• Clear syntax design: The syntax, or form, of the elements of a language has a
significant effect on the readability of programs. For example, use of special
words such as end if makes a program more readable.
• Support for abstraction: Programming language should provide facilities to define
and then use complicated structures or operations in ways that allow many of the
details to be ignored. Two types of abstraction are process (subprograms) and
data abstraction (structures, records, objects).
• Expressivity: Typically expressivity means that a language has convenient ways of
specifying computations. For example, in C, C++ and Java, the notation count++
180
Introduction to Computer Programming
is a more convenient and shorter way of incrementing count by 1 equivalent to
count = count + 1.
• Mechanisms to handle exceptions: This is the ability of a program to intercept
run-time errors or detect other unusual conditions, take corrective measures,
and then continue with normal execution. A good programming language should
provide mechanism to handle exceptions.
• Type checking: Type checking refers to testing for data type errors during program
compilation or run-time (execution). Because run-time type checking is expensive,
it is more desirable for a programming language to verify data type at compilt-time.
• Cost-effective: The total cost of a programming language can be evaluated in terms
of compiler cost, software development process, compilation time, implementation
platforms, programmer training and maintenance.
Activity 9.7: Qualities of a good program
List and discuss 4 characteristics of a good programming language.
Exercise 9.4
1. Explain the concepts: object-orientation, and logic programming paradigms.
2. Explain why knowledge of programming language characteristics can benefit the
whole computing community.
3. Explain the programming paradigm supported by F# programming language.
4. Explain why is it useful for a programmer to have some background in language
design, even though he or she may never actually design a programming language?
Unit Test 9
1. Differentiate between a computer program and software.
2. Explain how evolution of computers have influenced paradigm shift in computer
programming.
3. List three examples of object-oriented programming languages.
4. Differentiate between procedural programming and functional programming
paradigms.
5. Pascal and FORTRAN are examples of _______ generation programming
languages.
6. Procedural languages make it possible to break down a program into components
known as ___________ or ________.
7. A programming paradigm in which a program is executed in sequenced order
is known as _________.
181