Lesson 1 Structured Programming
Lesson 1 Structured Programming
LESSON ONE
This is a style in programming paradigm of dividing a problem into sub- problems, and sub –
problems into sub-sub- problems, and so on, (top down design) then solving each sub-sub –
problems individually to produce sub sub-solutions, and combining the sub-solutions to form the
solution (bottom up design). Therefore, a complex problem is broken up and made more
understandable, and thus easier to solve.
The most common techniques used in structured programming to solve all problems are called
constructs. Sequence, selection, and repetition constructs. These constructs are also called control
structures. Using these three basic control structures, it is possible to write standardized programs,
which are easy to read and understand.
The final computer program (consisting of all the function) will be the solution.
➢ Structured programming breaks up a problem into sub- problems and this makes problems
more understandable and therefore easy to solve.
➢ The solution to the problem is also modifiable in future. Having sections of something always
makes the whole thing modifiable.
➢ Structured programming also makes the program more efficient. When having many sub –
solutions(functions), we can at one time load.
➢ Having small sections of a program running saves memory space and also CPU’S.
a) How do we break the problems into sub- problems (top- down approach)
Given a programming problem, we first ask ourselves what results(output) is the program required
to produce, and then what inputs we require to get such outputs we list both these.
We then list the sequence of tasks that we need to do to convert the inputs into the outputs. In large
scale programming, each task may require other task, and this results to sub-sub-task.
input x
To specify the formula for computing the area of a rectangle, the pseudo code line might be
Syntax
Begin
Constants
Inputs
Processes/computation
outputs
End
Begin/End or Start/Stop
Input, Output
Shows Logic flow
condition? False
True
Decision making (test condition inside. If it is true, follow the down side
arrow. Else, follow the right-side arrow)
For example, the pseudo code statement for outputting the value of variable x (of integer
type) could be written as
Output x
This statement is understandable to a programmer of any language.
A C programmer is able to understand this algorithm statement and convert it into the C
statement.
printf(“%d”, x);
Count<<x;
A java programmer on the other hand, will write
[Link](x);
a) Design and write a program to compute the sum of two integer values.
b) Design and write a program to compute the average of three numbers.
c) Design and write program to compute the area and circumference of a
circle.
N.B More examples will be given by your tutor in class so do miss
it…….
1. Data Can be define as raw facts that the computer needs in its processing. It is the details
that are needed to process something (the information).
2. Information processed data.
3. Processing is the conversion of data into information. It involves activities such as;
• Computing a particular value out of a given set of data.
• Sorting a set of data.
• Rearranging of a set of data.
• Searching for a value in a set of data, etc.
Unsorted set of data Sorting the data The sorted set of data
Unarranged set of data Arranging the data The arranged set of data
A set of data Searching for a value in the The position of the value in
set the set.
A set of data Computing the Variance of The Variance
numbers
Employee’s details e.g. Computing the net pay The net pay
basic pay, grade, etc.
4. A Computer
A Computer is an electronic device for doing data processing. It is a hardware tool that
follows Instructions in it that instructs it on how exactly to do processing.
Computer is just a hardware tool that will do nothing unless instructed on what to do. It
requires instruction that instruct it on what to do. The term computer is however used to
refer to both the hardware and these instructions. Computer also does data storage.
5. Inputs and Outputs; Inputting and Outputting
The data that the computer receives from the user (through the inputs device e.g. keyboard)
is called inputs, while what is displayed to the user (through the outputs device e.g.
monitor) is called the outputs.
Receiving inputs from the user is called Inputting, while displayed the outputs to the user
is knows as Outputting.
6. Programming personnel
a) A computer Programmer-This is the person who develops computer programs. In
large scale programming, the programmer is however assisted by.
b) A system analyst-who obtains user requirements from the intended user of the program
(e.g. what outputs the user expects), analyzes the problem, write the requirements of
how the system should work (system requirements what inputs are required to
produce the outputs), and produces the design of the program.
c) A Database developer-designs the database in case the program is to be linked to
database.
d) A clerk-helps the programmer in clerical works eighties the actual program into
computer, carries out testing of the program, document the program etc.
e) A Software engineer- is a very important person in software development who is
responsible for ensuring the software being produced certifies the required
standards. The engineer also supervises and co-ordinates the overall process of
software development including analysis, design, program writing, testing and
maintenance.
f) An analyst programmer does the combined work of both the system analyst and the
programmer.
7. 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.
Examples of Programs
a) A program to compute the information shown above.
b) The calculator is one of the simple’s type of computer that contains some software
instruction.
c) System software’s.
8. 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.
The program created must specify in detail the logical steps to be taken & the method of processing
the data input into the computer in order to carry out the specified task.
The computer must be able to translate these instructions into machine-readable form when
arranged in a particular sequence or order.
EXAMPLE 3
In computing the net pay of an employee, the steps might be;
a) Input the basic salary, the grade, etc.;
b) Process the inputs as
i. Computes the deductions;
ii. Compute the allowances;
iii. Compute the net pay (basic salary-deductions+ allowances);
c) Output the net pay.
These steps are implemented as instruction or statements in the program. The program should
consist of a sequence of statements that instruct the computer on the exact steps, formulae and
logic of solving the problem.
The organization may either hire its own programmers to writes the programs or outsource
this work to a software development company.
2) General software
These are programs meant to be used for wide variety of applications and can be used by any
type of organization. They are also known as application packages. An application packages
contain set of programs for doing general work under a broad area of application. These
packages are typically commercial programs i.e. are produced by software development
companies for sale. i.e.
• Word processor.
• Spreadsheet.
• Database.
• Presentation.
• Multimedia programs etc.
Machine language is written using machine codes (binary digits) that consist of 0’s & 1’s.
The computer can readily understand Machine code 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.
Note. The computer can only execute instructions which are written in machine language. This
is because; it is the only language which the computer can understand. Therefore, any program
written in any other programming language must first be translated into machine language
(binary digits) before the computer can understand.
Assembly languages were developed in order to speed up programming (i.e., to overcome the
difficulties of understanding and using machine languages).
The vocabulary of Assembly languages is close to that of machine language, and its instructions
are symbolic representations of the machine language instructions.
Assembly language programs are easier to understand, use & modify compared to Machine
language programs.
Assembly language programs have less error chances.
To write program statements in Assembly language, the programmer uses a set of predefined
symbols (operation codes) called Mnemonic codes.
The code could be a 2 or 3 shortened letter word that will cause the computer to perform specific
operation. E.g., MOV – move, ADD - addition, SUB – subtraction, RD - read. Example;
RD PAT, 15 (read the value 15 stored in the processor register named PAT)
The Assembler accepts the source codes written in an Assembly language as its input, and
translates them into their corresponding computer language (machine code/ object code)
equivalent. Comments are incorporated into the program statements to make them easier to be
understood by the human programmers.
Very few computer programs are actually written in machine or Assembly language because of
the following reasons;
1. Low-level languages are difficult to learn, understand, and write programs in them.
2. Low-level language programs are difficult to debug (remove errors from).
3. The programs are very long; hence, writing a program in a low-level language is usually
tedious & time consuming
4. The programs are difficult to develop, maintain, and are also prone to errors (i.e., it requires
highly trained experts to develop and maintain the programs)
5. Low level languages are machine-dependent (specific), hence non-portable.
This implies that, they are designed for a specific machine & specific processor, and therefore,
cannot be transferred between machines with different hardware specifications.
6. They have a collection of very detailed & complex instructions that control the internal
circuiting of the computer. Therefore, it requires one to understand how the computer codes
internally.
7. Relating the program & the problem structures is difficult, and therefore cumbersome to
work with.
8. It is not easy to revise the program, because this will mean re-writing the program again.
a) Commercial languages
They are meant for commercial applications. An example is COBOL (Common Business
Oriented Languages).
b) Scientific languages.
They are meant for scientific applications. They include FORTRAN(FORmulaTRANlation),
ALGOL(ALGorithmic Oriented Language).
c) Special Purpose languages
These include CSL- for simulation application, ADA- for real time application, SQL
(Structured Query Languages)- for Database querying, prolog- for programming in logic.
d) General Purpose Languages.
These are multi-purpose languages that can cope with different types of applications. They
include:
❖ BASIC (Beginners All Purpose Symbolic Instruction Code). A language used since
many years ago, and which has been very simple for beginners of programming to
learn. Another language Visual Basic, was developed out of this language. Visual
Basic is a windows-based application popular for creating user- friendly graphical user
interface applications. It is event based, meaning that the program reacts to user’s
events including mouse click, key press, form load, etc. it is popular with creation of
business-oriented computer application. Another language that operates very similar to
Visual Basic is Delphi.
❖ Pascal –A language popular for training of beginners of programming because of its
nearness to English language, thus easy to learn.
❖ C Language- a popular high level, yet ’low level’ language that also provides for
programming at hardware level (low level). its popular with development of the system
software e.g. operating systems.
❖ C++ Language- it is an extension of C Language to provide for object-oriented
programming is a technique of grouping related things together into classes into a class,
and specifying the type of data that can be stored for any of the objects and the operation
that can be applied to each object. Any occurrence of class (a member of the group) is
an object.
❖ Java- an object-oriented programming language that is popular for creation on
networks.
Advantages of HLL
❖ HLL are simple to use.
❖ They are machine independent i.e. the program can be executed by different types
of computer hardware.
❖ A High-level statement can be translated into many machine language statements.
• Lack of portability: program that runs on one machine can’t run on the other.
• Programs are generally longer than equivalent programs written in high level languages.
• Programmer requires intimate knowledge of the internal working of the particular
computer he is working on.
• A High-level program must be translated into the equivalent machine language program,
making the speed of execution slower than a machine language program.
a) Source program (source code)-Source program refers to the 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.
b) Object code (object program). -Object code refers to the program code that is in
machine-readable (binary) form. Object code is the code 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 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.
In addition, translators check for & identify some types of errors that may be present in the program
being translated, e.g., Syntax/grammatical errors. They will produce error messages if there is a
mistake in the 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.
1. 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.
- 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.
2. 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 line,
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.
3. 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.
For a given machine, each language requires its own Compiler. E.g., for a computer to be able
translate a program written in FORTRAN into machine language; the program must pass through
the FORTRAN compiler (which must ‘know’ FORTRAN as well as the Machine language of the
computer).
The object code file can be made into a fully executable program by carrying out a Linking
process, which joins the object code to all the other files that are needed for the execution of the
program. After the linking process, an executable file with an .EXE extension is generated. This
file is stored on a storage media.
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.
Differences between Compilers and Interpreters
Interpreter Compiler
1. Translates & executes each statement of the 1. Translates all the source code statements at
source code one at a time. once as a unit into their corresponding
The source code instruction is translated & object codes, before the computer can
immediately obeyed by the computer execute them.
hardware before the next instruction can be A Compiler translates the entire source
translated. program first to machine code, and then the
code is executed by the CPU.
(Translation & execution go together).
(Translation & execution are separate
phases)
2. Translates the program each time it is 2. Compiled programs (object codes) can be
needed for execution; hence, it is slower saved on a storage media and run when
than compiling. required; hence they execute faster than
interpreted programs.
3. Interpreted object codes take less memory 3. Compiled programs require more memory
compared to compiled programs. as their object files are larger.