CIT 302 COMPUTER PROGRAMMING 22-1
CIT 302 COMPUTER PROGRAMMING 22-1
1
CHAPTER ONE
OVERVIEW OF PROGRAMMING LANGUAGES
1.0 Introduction:
Programs and Programming: A program is a set of instructions. When you write down
directions to your house for a friend, you are writing a program. Your friend ―executes‖ that
program by following each instruction in turn.
Every program is written in terms of a few basic operations that its reader already
understands. For example, the set of operations that your friend can understand might include
the following: ―Turn left at Christ Church Junction,‖ ―Go forward three blocks,‖ and ―If you
get to the gas station, turn around.‖
Computers are similar but have a different set of operations. Some operations are
mathematical, like ―Take the square root of a number,‖ whereas others include ―Read a line
from the file named data.txt‖ and ―Make a pixel blue.‖
Defining new operations and combining them to do useful things is the heart and soul of
programming. It is also a tremendously powerful way to think about other kinds of problems.
In the same way, there are many programming languages, and they all can add numbers, read
information from files, and make user interfaces with windows and buttons and scroll bars.
The instructions look different, but they accomplish the same task.
To write a program (tells what to do) for a computer, we must use a computer language. Over
the years computer languages have evolved from machine languages to natural languages.
The following are the summary of computer languages:
i. Machine Languages
ii. Symbolic Languages
iii. High Level Languages
i. Machine Language:
Among all the Computer programming languages, only the machine language is in machine-
executable form. All other languages must be translated into O's and l 's, the only things
understood by the computer.
2
Disadvantages of Machine Language
1. Machine dependent
2. Programming is very difficult
3. Difficult to understand
4. Difficult to write bug free programs 5) Difficult to isolate an error
3
3 Translator Not Needed Needed (Assembler) Needed (Compiler)
4 Execution Time Less Less High
5 Languages Only one Different Different
Manufacturer Languages
6 Nature Difficult Difficult Easy
7 Memory Space Less Less More
2. Readability:
One of the most important criteria for judging a programming language is the ease with
which programs can be read and understood. Language constructs were designed more from
the point of view of the computer than of computer users.
3. Reliability:
A program is said to be reliable if performs to its specifications under all conditions. Type
checking. Readability and writability both influence reliability
4
4. Ease of Program Verification (Reusability):
The reusability of programs written in a language is always a central concern. A program is
checked by various techniques like
i. Formal verification method
ii. Desk checking
iii. Input – Output test checking
A language that makes program verification difficult may be more troublesome to use.
5. Portability of programs:
The concept of portability in programming is that a program should be capable of being
transferable to a different computer platforms with a minimum modification, if any at all
6. Maintainability: By this, we mean that you should be able to modify the program
when the need arises.
Language Translators
These are the programs which are used for converting the programs in one language into machine
language instructions, so that they can be excuted by the computer.
1. Compiler: It is a program which is used to convert the high level language programs into
machine language or a Compiler translates an entire program into machine language before the
execution of the program. A Compiler usually translates the SOURCE program into another program
called the OBJECT program which is the machine language version of the source code. With the
object program created by your compiler, you will never use the source program again except when
you want to modify it. Generally, a compiled program runs faster than an interpreted program.
2. Assembler: It is a program which is used to convert the assembly level language programs
into machine language
3. Interpreter: Some languages are interpreted by converting the "source program" into machine
language as the program is being executed. Interpreters translate code line-by- line which therefore
makes them run slowly than other translators. For example, sonic BASIC language versions only
interpret programs instead of compiling them. However in Turbo Basic, KBASIC or BASIC 4GL you
can compile the BASIC source code into an executable code.
5
CHAPTER TWO
PRINCIPLE OF GOOD PROGRAMMING: CONCEPTS OF STRUCURED,
MODULAR AND DEFENSIVE PROGRAMMING
The structured programming design is a programming tool developed in the 1960s as a way
of defining the elements of a problem and as at today it's the best approach to all computer
programming tasks.
Structured programming gives room for well-thought-out program logic and provides an
attempt to keep programs as simple and straight forward as possible. Also structured
programming
i. increases programmer productivity
ii. increases program clarity by reducing complexity
iii. reduces program testing time
iv. reduces program maintenance time and effort.
i. Sequence Structure: In sequential structure or what you can also call sequential
logic, the steps are executed one after another as serial operations. This can be illustrated by
the following flowchart:
Process A
Process A
Process A
Looking at the above flowchart, there are two arrows, one at the at the bottom the last process
box. They respectively represent the ENTRY and EXIT points of the program segment. It is
good to state basic guideline of structured programming is that each module should have only
one entry point and one exit point. For this reason, a program that has only one entry and one
exit is called a proper program.
Condition True
Process
False
In the above flowchart, if the condition is satisfied, then the instruction(s) in the Process box
(or what is also called ACTION BLOCK) is/are executed. If the condition is not satisfied, the
control transfers to the next instruction following the checking of the condition.
Double Alternation:
The structure is illustrated below:
False True
Condition
ELSE THEN
process
process
Exercise 1:
Construct the pseudocode for the above structure.
Answer
IF condition THEN
Action — block – 1
7
ELSE
Action block — 2
ENDIF
Where action block 1 constitutes the THEN process and action-block-2 represents the ELSE
process.
Multiple Alternation:
The structure is seen in the following flowchart using three conditions:
False
False
False
THEN PROCESS -
Condition - 4
4
False
ELSE Process
Looking at the above flowchart, you will see that if none of the four conditions is satisfied,
then the process in the ELSE box will be executed.
8
Basically, structured programming employs Modular Approach and Top-Down Principle.
A module is a collection of variables and functions that are grouped together in a single file.
The variables and functions in a module are usually related to one another in some way.
When referring to files and repositories, modularity can occur on a variety of levels.
1. Libraries in projects
2. Function in the files
3. Files in the libraries or repositories
It all comes down to making blocks, and each block is made with the assistance of other
blocks. Every block in itself is solid and testable and can be stacked together to create an
entire application. Therefore, thinking about the concept of modularity is also like building
the whole architecture of the application.
All the object-oriented programming languages like C++,Java, etc., are modular
programming languages
2. Code is easier to test: Some functions in software carry out fewer tasks than others,
as well as functions that carry out a lot of activities. It is simpler to test software if it is easily
divided into separate pieces using modules. In order to make it bugfree, we might concentrate
on the risk functions; during testing and require more test cases.
3. Reusability: Sometimes a single bit of code is used a cross our entire program.
Modularity offers us the benefit of reusability, allowing us to pull our code from anywhere
via interfaces or libraries rather than repeatedly copying and pasting it. The idea of reusability
also makes our program smaller.
4. Faster fixes: Let's say that a payment option in an application has a bug that has to be
fixed. Because we are aware that the payment code will be contained in a separate function
and will only be corrected in that function, modularity can be quite helpful. As a result,
finding and fixing errors with modules is more- easier and more maintainable.
5. Low-risk update: In modular programming, the things that utilize it are prevented
from altering the library by a defined layer of APIs. There is little chance of someone
breaking the code until the API is changed. Without defined APIs, for instance, someone
9
could mistakenly break something if they altered a function they believed was only used by
that library but was actually utilized else-where.
6. Easy collaboration: In the team, various developers each work on a specific piece of
code. Conflicts can possible when there is a git merge. If the code is broken out into
numerous functions, files, repositories, etc., this conflict can be minimized. We can also give
a team member control over particular code modules so that they can be divided into smaller
jobs
3. To ensure that other program modules are not impacted, careful documentation is
needed
4. Modular programs use more memory and longer execution times. Integrating various
modules into a single program may not be at ask because different people working on the
design of different modules may not have the same style
Defensive programming
While defensive programming is an approach in which the programming assumes that there
may be undetected faults in a code. The methods for defensive programming include data
control, (type, field limits, plausibility checks etc).
3. Keep your code as simply as possible because complexity breeds bugs. Functions
should be viewed as a contract between the user and the coder. The coder guarantees that the
function will execute only a specific task and if it cannot do that task (divide by zero), it can
notify the calling function that error has occurred.
CHAPTER THREE
10
11