Elementary Programming (1)
Elementary Programming (1)
PROGRAMMING
PRINCIPLES
2
DEFINITION OF TERMS
IN ELEMENTARY
PROGRAMMING
PRINCIPLES
3
Computer Program
Programming:
It is a process of writing a set of coded instructions that
can be interpreted and executed by a computer in order
to solve a particular task.
Programming Languages
Source Code:
The term Source program refers to program
statements that the programmer enters in the
program editor window, and which have not yet been
translated into machine-readable form.
Object Code:
This refers to a source code that has been
converted to machine readable format.
Language Translators
10
Language Translators
Functions of an Assembler:
▰ Converts symbolic operation code (mnemonic) to
an object code.
▰ Allocates space in memory for the object code.
▰ Identify errors and produces a list of all errors
detected.
▰ It assigns memory locations to the names the
programmer uses.
14
Example;
For a computer to be able translate a program written in
FORTRAN into machine code; the program must pass
through the FORTRAN compiler.
15
Functions of a Compiler:
▰ Translates the entire source code to object code.
▰ Allocates space in memory for the object code.
▰ Generates executable files.
▰ Saves executable files in a storage medium.
▰ Identifies errors detected.
▰ Stops compilation if errors are detected in the
source program.
16
Interpreter:
NOTE
▰An interpreter works with
spoken language, whereas
a translator works with
written materials.
18
Disadvantages of Machine
language.
▰ It is not portable since it is designed for a
specific computer architecture.
▰ The process of writing the program is
tedious and time consuming.
▰ It is difficult to read, understand and
debug the lines of codes.
“
Machine language has
more disadvantages
than advantages, this
led to the development
of the assembly
language.
”
28
• Assembly languages were developed in order
to speed up programming (i.e., to overcome
the difficulties of understanding and using
machine languages).
• It uses symbolic representations known as
Assembly
Mnemonics in designing the program.
Language • Mnemonics are easy to remember and more
(2nd understandable than the machine code.
Generation • Example;
Languages): • RD PAT, 15 (read the value 15 stored in the
processor register named PAT)
• SUB PAT, 10 (subtract 10 from the value in
register PAT)
29
Disadvantages of Assembly
Language.
▰ It is very tedious to write a
program.
▰ Difficult to read, understand and
debug the lines of codes.
▰ It is not portable since it machine-
dependent.
31
Advantages of Structured
programming
▰ It is easier to read and modify the program.
▰ Enables the user debug errors in a section of
the program.
▰ Enables the user test the program one
module after another making programming
more flexible.
36
Examples of 5GLs.
PROGRAM
DEVELOPMENT
“
Program
development is the
process of creating
software.
”
PROGRAM DEVELOPMENT
▰ There are six stages of program
development.
Problem recognition Documentation
Problem definition
Program design
Program coding
Program testing
Program implementation
Problem recognition
▰ This refers to the understanding and
interpretation of a particular problem. To
understand a problem one has to look for key
words such as compute, evaluate, compare
etc. a programmer identifies problems in the
environment and seeks to solve them by
writing a computer program that would
provide the solution.
Circumstances that can cause the
programmer to identify a problem
▰ Opportunity to improve the current
program.
{Procedure name}
End
Selection/decision
▰ This structure is used to branch, depending on
whether the condition returns a value of True of False
(Yes or No).
For example
IF < Condition >
Then Action 1
Else Action 2
Endif.
IF……THEN……ELSE
Nested IF
IF……….THEN
a. 80 marks Grade A
b. 60 marks Grade B
c. 50 marks Grade C
d. 40 marks Grade D
General format of Nested IF
General Format The pseudo code segment will
be:-
IF <Conditions> Then
If Marks = 80 THEN
Statement Grade = “A”
ELSE ELSE
IF <Condition> If Marks = 60 THEN
Then Grade = “B”
Statement ELSE
ELSE If marks = 50 THEN
IF Grade = “D”
<Condition>Then Endif
Statement Endif
Endif
Endif
Endif
Endif
End
Endif
103
CASE SELECTION General format
CASE x of
Label 1 : Statement1
Label 2 : Statement2
ITERATION (LOOPING)
REPETITION
▰ THE-WHILE-
DO LOOP
▰ REPEAT……….UN
TIL LOOP
The FOR
LOOP
Iteration (looping) repetition
Example
To withdraw money using an ATM a
customer must have a balance in his/her
account.
General format of THE-WHILE DO
LOOP
General format
While <condition>
Do
Flowchar
t Statement
End while
Withdraw cash
update account
Pseudo code
segment
While balance >0
do
Withdraw
REPEAT……….UNTIL LOOP
Pseudo code
FOR count = 1 to 10 DO
writeln “Enter a number (N)”
Readln N
Sum = sum + N
End FOR
Display Sum
2. Format for the ‘For’ loop that counts from upper limit down to
lower limit
For loop variable = Upper limit Down To Lower Limit Do
Statements
EndFor
Flowchart for a For Loop that
counts upward
Statement
Flowchart for a For Loop that counts
downwards
Statement
Examples of complex pseudo
codes 1
▰ Ushirika Society pays 5% interest on shares
exceeding Kshs 10,000 and 3% on shares that do
not meet this target. However no interest is paid
on deposits in the member’s bank account. Design
a pseudo code for a program that would:
a. Prompt the user for shares and deposit of a particular
member.
b. Calculate the interest and total savings.
c. Display the interest and total savings on the screen
for a particular member.
Pseudo code of examples 1
Pseudo code
Start
Input Name, share, Deposit
If share > 10000 THEN
Interest = 0.05*shares
ELSE
Interest=0.03*shares
Endif
Total savings = deposits + Interest +
shares
Print Name, Totalsaving, Interest
Stop
Flowchart of example
1
Interest = 0.03 *
shares
Interest = 0.05 *
shares