COMPILERS
Convert high level language program to machine
code by taking instructions one by one..
ADVANTAGES
Compiled program can be run as often as
required.
Compiled program run quickly and make
good use of computer hardware.
DISADVANTAGES
Compilation process takes a long time (slow)
It is complicated
A number of stages are required for a larger
program
INTERPRETERS
Convert high level language program to
machine code by taking ALL instructions at
once.
ADVANTAGES
Interpreting process is simpler than
compilation.
DISADVANTAGES
A machine code version of a program is not
produced
An interpreter is always needed every time a
program is run
An interpreted program is usually slower than
a compiled one.
Assembly Language
A programming language which is easier
to use than machine language but still
refers directly to the computer hardware.
It is a low level language
Assembly language uses simple word like
ADD,SUB for writing instructions in
programs
Assembler is used to convert programs in
assembly language to machine code.
STRUCTURED
PROGRAMMING
STRUCTURED PROGRAMMING
(a.k.a Modular Programming)
It is a computer programming in
which the statements are
organized in a specific manner to
minimize error of
misinterpretation.
It enforces a logical structure on
the program being written to
make it more efficient and easier
to understand and modify.
Characteristics of structured
Programming
The code is broken into modules
(sub systems).
Each module has one and only
one function
The modules are loosely
coupled (less dependent on
another).
TOP-DOWN APPROACH
(Step-wise design)
The breaking down of the system to gain insights
into its compositional subsystems.
The system is divided into subsystems, which also
can be divided into other subsystems and so on.
The idea of this technique is that the sub problems
are easier to solve than the whole problem at once.
The overview of the whole system is first
established and then addresses the specific and
complex issues of the implementation of various
sub-systems.
Top-down is used as a synonym of analysis or
decomposition.
Structured
Programming
Techniques
Top-down
Bottom-up
CASE
ADVANTAGES OF TOP
DOWN DEVELOPMENT
Fewer operation errors since each module is
processed separately.
Less time consuming; each programmer is only
involved in a part of the big project.
Easy to maintain; if there is an error in the output, it is
easy to identify the error generated from which
module of the entire program
Progress can be easily measured; completion of
certain modules indicates how well the project is
coming along.
The processing is optimized; each programmer has to
apply their own knowledge and experience to their
parts (modules)
DISADVANTAGES
The benefit of the solution is not
realised at the early stages.
The implementation cost is likely
to be higher.
BOTTOM-UP DESIGN
In a bottom-up approach the individual
base elements of the system are first
specified in great detail. These
elements are then linked together to
form larger subsystems, which then in
turn are linked, sometimes in many
levels, until a complete top-level
system is formed.
This strategy often resembles a "seed"
model, whereby the beginnings are
small but eventually grow in
complexity and completeness.
ADVANTAGES
This approach is suitable for
smaller programs.
It promotes code re-use; when
you write two or more programs,
many of the instructions you
wrote for the first programs will
be useful for the succeeding ones.
Makes program easier to read
DISADVANTAGES
Its focus is not on specific
requirements; thus the results
may not fit a given need
(requirement).
It is difficult to construct quality
programs with this approach.
CASE: (Computer-Aided
Software Engineering)
It is the use of tools to assist in the
development and maintenance of
software
These tools are known as CASE tools.
The CASE functions include analysis,
design, and programming. CASE tools
automate methods for designing,
documenting, and producing structured
computer code in the desired
programming language.
ADVANTAGES
It helps with standardization of
notations and diagrams
Less time consuming and less
effort needed
Allows the re-use of models or
their components
DISADVANTAGES
Limited flexibility of documentation
It may lead to restriction to the
tool’s capability
Completeness does not mean
compliance with the requirements
Costs associated with the use of
these tools are high- purchase &
training
HOMEWORK
Describe the following
1.Algorithm
2. Flowcharts
3. Pseudo code
4. Describe the three control structures
in programming
Sequence
Selection
Iteration
ALGORITHMS
PSEUDOCODE &
FLOWCHARTS
ALGORITHMS
A sequence of instructions or steps required to
accomplish a task.
In short, its a sequence of steps required to
accomplish a task. For example, to watch a movie
ina cinema, you need to to first buy a ticket and
then watch the movie in the cinema. You cant
directly enter the cinema hall without buying the
ticket.
You will notice that the algorithm follows a certain
steps to accomplish the task. The steps can be
categorised in three phases:THE INPUT,
PROCESS,OUTPUT PHASE (IPO)
Example: Purchasing
an item from a store
INPUT
Accept the item name
Accept item quantity
Read the price
PROCESS
Calculate the total value (Quantity*Price)
Calculate VAT(12% of total value)
Calculate the Grand total (Total Value + VAT)
OUTPUT
Display the total on the screen.
CHARACTERISTICS OF
ALGORITHMS
an algorithm ends after a fixed number of
steps
each step in an algorithm clealy specifies the
action to be perfomed.
The steps in an algorithm specify basic
operations. These operations could include
mathematical calculations, input/output
functions and logical operations.
The algorithm should accept input data, in a
defined format, before it can be processed by
the given instructions.
An algorithm generates one or more outputs
after the input is processed.
Algorithms can be represented
using natural language, formal
programming languages, pseudo
code or flowchart.
Pseudo code:
1. Pseudo code: is an artificial and informal
language that helps programmers develop algorithms.
It represents an algorithm in English language. It is
used as an alternative to a flowchart. Pseudo code is a
"text-based" detail (algorithmic) design tool. Instead
of using the flow chart, one can use a Pseudocode.
After it has been developed, it can be written in a
high-level language. A number of keywords used in a
Pseudocode are:
begin…end: used to start and finish a Pseudocode.
input (accept): obtain input form a user.
display: this is used to present the result or an output.
If…else, while, etc: used in decision making.
Examples of
pseudocodes:
A pseudocode to calculate the average of
two numbers
Begin
Accept 1st number
Accept 2nd Number
Calculate the average ((num1 +
num2)/2)
Display Average
End
Example 2
Algorithm to purchase from a store
Begin
Accept item name
Accept the quantity
Accept the unit price
Calculate the total value
Display total value
End
Advantages of Using
Psedocode
It is easier and faster to write
Errors can be detected and
changes be made easily.
It can be converted to a program
using any programming
language.
Limitations of Pseudo
code
It does not provide a graphical
representation therefore it might
be difficult to understand
Flowchart:
A graphical representation of an
algorithm.
It helps to analyse the problem
effectively as they act as a guide during
the program development phase.
It consists of symbols to represent
steps of an algorithm.
Each symbol represent a type of
activity or a step.
Some standard
symbols of a flowchart
RULES OF FLOW
CHARTING
The entire logic of a flowchart should be presented
using standard symbols
They should be clear, precise and easy to follow.
Flowcharts can only have one start point and one
end point
All necessary inputs should be listed in a logical
order
The start and stop should have only a single flow
line.
The decision symbol should have one flow line
connecting to the previous symbol but two flow
lines connecting to the next symbol with a
possible solution. Yes/No, true/false
CLASS EXERCISES (use
both a flow chart & a
Pseudocode)
Write an algorithm to accept age form the user. If the age is zero or negative,
the message “age cannot be zero” and “age cannot be negative” should be
displayed respectively.
Write an algorithm to accept two numbers, calculate their product, and
display whether the result is an even or odd number.
Write an algorithm to accept a year form the user and determine whether it is
a leap year or not.
Write an algorithm that allows the user enter some marks and then display
grade given the table below:
EXAM GRADE
80 - 100 A
70 – 79 B
60 – 69 C
50 – 59 D
0 – 49 E
Write an algorithm that will sort any 2 numbers in descending order.
Write an algorithm that will display “PASS”, if the grade is higher than 60, and
“FAIL” if the grade is less than 60.
CATEGORIES OF
ALGORITHMS
An algorithm must have the ability to
alter the order of its instructions.
A control structure makes this
possible.
A computer can process a program in
one of the following 3 ways
Sequence
Selection
Repetition
SEQUENCE
This is when instructions are
executed in order
FLOW OF EXECUTION
e.g.
Input (x,y)
Average=x+y/2
Output (x, y, average)
SELECTION
This type of control structure is
conditional (‘question asking’).
It requires a decision to be made
by asking questions like
true/false, Yes/No
FLOW OF EXECUTION
The if Statement:
IF condition Then
Value or Statement if true
ELSE
Value or statement if false
The Nested If Statement:
IF condition THEN
Statement
ELSE IF condition THEN
Statement
ELSE IF Condition
Statement
Else
Statement
ENDIF
REPETITION CONTROL
STRUCTURE
The repetition control structure is
also known as the looping or
iteration structure.
Looping is the process of
repeatedly executing one or more
steps of an algorithm or program.
It is essential in programming, as
most programs perform
repetitious tasks.
Parts of a loop
1) The loop termination decision - determines when
The loop termination decision - determines when
(under what condition) the loop will be terminated (stopped).
It is essential that some provision in the program be made for
the loop to stop; otherwise, the computer would continue to
execute the loop indefinitely - a loop that doesn't stop is
called an endless loop or infinite loop; when a program gets
stuck in an endless loop, programmers say that the program
is "looping".
(2) The body of the loop - the step or steps of the algorithm
that are repeated within the loop. This may be only one
action, or it may include almost all of the program
statements. Important note: some action must occur within
the body of the loop that will affect the loop termination
decision at some point.
(3) Transfer back to the beginning of the loop - returns
control to the top of the loop so that a new repetition
(iteration) can begin.
TYPES OF LOOPS
While loop
For loop
Repeat…until loop
Go to loop
The While loop
The while loop executes algorithm
for a fixed number of times.
This loop will continuously
execute the statements in the
body as long as a given condition
is true.
The while loop can be thought of
as a repeating if statement.
The While loop Syntax
(Pseudo code)
……
WHILE (condition) DO
Statements/body
ENDWHILE
The While loop Syntax
(Flow chart)
Example of while loop
x=0, y=0, z=0
While x<4 DO
x=x+1
y= y+3
z= 2x + 1
Endwhile
Output (x, y, z)
start
X=0, y=0, x=0
X= x+1
Y = y+3 Output x, y, z
Z = 2x +y
Is x>4? NO
YES
Stop
TRACE TABLE
x y z
For Loop
The for loop execute a given set of
instructions a limited number of times
with little or no changes.
This loop will start with giving a
variable a predefined value and then
keep on executing the body part of the
loop as long as the given condition is
true. Every time the loop is executed,
the variable gets a new value.
A for loop is a another form of a while
loop
The for loop syntax
For (INITIALIZATION ; CONDITION ;
Update statement)
BODY/statements
EndFor
Repeat…Until Loop
It is similar to a while loop in which a
statement or set of statements to be
executed for a fixed number of times is
known.
The only difference is that, the condition
in a repeat…until loop is evaluated after
executing the body. Therefore is called
a post-test and a while loop is a pre-test
because the loop condition is evaluated
before executing the body of the loop
Syntax of Repeat…until
loop
Repeat
Statement(s)
Until (condition)
The other difference with a
Repeat...Until loop is that the loop
continues until the condition is
TRUE.
In a while loop the loop continues
until the condition is FALSE.
Example
Repeat
Enter password
Until (Password=QWERTY)
Print “Correct Password. Welcome!”
Programming Terms
A program error is a mistake in a program
A BUG: It is a program error
DEBUGGING: Removing error(s) from the
program
SYNTAX: Rules of a programming
language.
Syntax Error: Results when the
programmer does not follow the
programming language rules
Logical Error: Results when the sequence
of instructions is not correct
Constant: Quantity that does not change
Variable: A quantity that has different values during
execution of program.
Initialization: assigning initial values to certain program
variables e.g. Sum =0, Maxlevel = 0; Total = 0
Looping (Repetition): Performing the same sequence of
instructions a number of times
Developing a counter: Counting the number of times a
particular program segment or process is performed.
Developing a sum: Determining a running total of
numbers as they are input or produced within a program
loop