0% found this document useful (0 votes)
24 views

CIT 302 COMPUTER PROGRAMMING 22-1

Computer programming Lecture note for SICT department in FUTO
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

CIT 302 COMPUTER PROGRAMMING 22-1

Computer programming Lecture note for SICT department in FUTO
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

CIT 302 COMPUTER PROGRAMMING

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.

1.1 What’s a Programming Language?


If you understood what a program is, then we will say: the act of writing programs is called
programming. There are hundreds of programming languages, which can be used to write
computer programs and following are a few of them: Java, C, C++, Python, PHP, etc

Programming language have evolved from assembly language to procedure-oriented


language, structured programming and data abstraction, object oriented, functional and
relational paradigms.

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.

Advantages of Machine Language


1. High speed execution
2. The computer can understood instructions immediately
3. No translation is needed.

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

ii. Symbolic Languages (Assembly Language):


A special program called an Assembler translates symbolic code into the machine language.
Hence they are called as Assembly language. These languages were known as symbolic
languages. Because a computer does not understand symbolic language it must be translated
into the machine language.

Advantages of Symbolic Languages


1. Easy to understand and use
2. Easy to modify and isolate error
3. High efficiency
4. More control on hardware

Disadvantages of Symbolic Languages


1. Machine Dependent Language
2. Requires translator
3. Difficult to learn and write programs
4. Slow development time 5) Less efficient

iii. High Level Languages:


High Level Languages are user-friendly languages which are similar to English with
vocabulary of words and symbols. These are easier to learn and require less time to write.
They are problem oriented rather than ‗machine‘ based. Program written in a high-level
language can be translated into many machine languages and therefore can run on any
computer for which there exists an appropriate translator

Advantages OF High Languages


1. Easy to write and understand
2. Easy to isolate an error
3. Machine independent language
4. Easy to maintain
5. Better readability
6. Low Development cost
7. Easier to document 8) Portable

Disadvantages of High Level Languages


1. Needs translator
2. Requires high execution time
3. Poor control on hardware
4. Less efficien

Difference between Machine, Assembly, High Level Languages


S/N Feature Machine Assembly High Level
1 Form 0‗s and 1‗s Mnemonic codes Normal English
2 Machine Dependent Dependent Dependent Independent

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

Reasons for Studying of Programming Languages


1. Improved background for choosing appropriate languages:
Many programmers when given a choice of languages for a new project, continue to use the
language with which they are most familiar, even if it is poorly suited to the project. If these
programmers were familiar with a wider range of languages, they would be able to choose the
language that includes the features that best address the characteristics of the problem at
hand.

2. Increased ability to learn new languages


In software development, continuous learning is essential. The process of learning a new
programming language can be lengthy and difficult, especially for someone who is
comfortable with only two or more languages. Once a thorough understanding of the
fundamental concepts of languages is acquired, it becomes far easier to see how these
concepts are incorporated into the design of the language being learned.

3. Better understanding the significance of implementation


An understanding of implementation issues leads to an understanding of why languages are
designed the way they are. This knowledge in turn leads to the ability to use a language more
intelligently, as it was designed to use. We can become better programmers by understanding
the choices among programming language constructs and consequences of those choices.

4. Better use of languages that are already known


By studying the concepts of programming languages, programmers can learn about
previously unknown and unused parts of the languages they already use and begin to use
those features

Features of a good programming language


1. Simplicity:
A good programming language provides both a conceptual framework for algorithm planning
and means of expressing them. This means that it should provide a clear, simple and unified
set of concepts that can be used as primitives in developing algorithms. This attribute
therefore is called conceptual integrity.

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

Programming language should be portable, it means it should be easy to transfer a program


from where it was developed to any other platform.

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.

Comparison between a Compiler and Interpreter


S/NO COMPILER INTERPRETER
1 A Compiler is used to compile an entire An interpreter is used to translate each line of
program and an executable program is the program code immediately as it is entered
generated through the object program
2 The executable program is stored in a disk The executable program is generated in
for future use or to run it in another RAM and the interpreter is required for each
computer run of the program
3 The compiled programs run faster The Interpreted programs run slower
4 Most of the Languages use compiler A very few languages use interpreters.

5
CHAPTER TWO
PRINCIPLE OF GOOD PROGRAMMING: CONCEPTS OF STRUCURED,
MODULAR AND DEFENSIVE PROGRAMMING

Concepts of structured, modular and defensive programming


Structured Programming is a technique for organizing programs in a hierarchy of modulus.
Each module had a single entry and a single exit point.

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.

In structured programming, Control is passed downward through the structure without


unconditional branches (e.g., goto statements) to higher level of the structure. Only three
types of structures were used: sequential, conditional branch and iteration.

iii. Iterative Structure.

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.

ii. Selection Structure:


The selection structure uses conditions and based on decision taken by the computer after
comparison of data, one of the available alternatives is selected. For this reason, it is also
6
called alternation structure or Conditional structure. It is also known as the IF structure. The
selection structures can be categorized into
i. Single Alternation
ii. Double Alternation
iv. Multiple Alternation.

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.

The pseucode is as follows:


If Condition THEN
Action-block statement(s)
END IF

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:

True THEN PROCESS - 1


Condition - 1

False

True THEN PROCESS - 2


Condition - 2

False

True THEN PROCESS - 3


Condition - 3

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.

Modules in programming glanguages


What is modular programming?
Modular Programming is a situation where a program is divided into parts or modules for
easy development and maintenance. Examples of modular programming languages are object
oriented programming languages like Java, C++, etc.

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

Advantages of modular programming


1. Code is easier to read: Working with modular programming makes code easier to
read because functions, as opposed to monolithic scripts, carry out different duties. If we pass
arguments and variables in to several functions, modular programming can occasionally
become a bit complicated. To prevent any issues, the use of modules should be done
carefully. Functions must to be clear, concise, and informative.

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

Disadvantages of modular programming


1. Modular programming calls for more time and money to produce a product

2. The challenge of combining all the modules is difficult

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).

Rules of defensive programming


1. Never assume anything. All input must be validated against a set of all legal inputs.
Then determine the action you take if the input is correct.

2. Use coding standards

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

You might also like