Chapter 3 Elementary Programming Principles
Chapter 3 Elementary Programming Principles
Elementary programming
principles
Form three work
1
Introduction
Human beings have evolved
from the stone age to a highly
sophisticated and advanced
society by inventing things like
the wheel, fire, transistors and
today’s ultra modern devices
like computers. The idea of
computers started way back in
the nineteenth century. The first
generation computers called
Electronic Numeric Integrator
And Calculator (ENIAC 1) were
operated by plugging wires into
a control panel that resembles
the old telephone switchboards.
Computer programming
A program – is an organised list of statements
(instructions) that when executed, causes the
computer to behave in a predetermined manner
or carry out a defined task.
Programming – refers to the process of
developing computer (instructions) programs
used to solve a particular task.
A computer program – is designed using a
particular programming language. Each
language has a special sequence or order of
writing characters usually referred to as syntax.
Terms used in programming
Source program
Object code
Translator
Assembler
Interpreter
Compiler
Source program
This refers to the program code that the
programmer enters in the program editor
window that is not yet translated into
machine-readable form.
Object code
This refers to the program code that is in
machine-readable. i.e. a source code that
has been translated into machine
language.
Translator
These are programs that
translates/convert the source program into
object code. E.g. assemblers, compliers,
interpreters etc.
Assembler
An assembler translates a program written
in assembly language into machine
language,
Interpreter
This translates the source program line-
by-line, allowing the CPU to execute one
line before translating the next. The
translated line is not stored in the
computer memory, hence every time the
program is executed, it has to be
translated.
Complier
This translates the entire source program
into object code. The compiler translates
each high-level instruction into several
machine code instructions in a process
called COMPILATION and produces a
complete independent program that can
be run by the computer as often as
required without the original source
program being present.
Difference between the interpreters and compilers
Interpreters Compilers
Translates the source program Translates the entire source
one statement at a time. code at once before execution.
Translates the program each Compiled program (object
time it is run hence slower than code) can be saved on a
compiling. storage media and run as
required, hence executes
faster than interpreted
programs.
Interpreted object code takes Compiled programs require
less memory compared to more memory as the object file
compiled program. are larger.
Level of programming languages
There are 2 major levels namely;
Low-level languages.
High-level languages.
Low-level languages
These languages are classified as low
because they can be easily understood by
the computer directly or they require little
effort to translate into computer
understandable form. These languages
are hardware oriented and therefore they
are not portable. i.e. a program written for
one computer cannot be installed and
used on another.
High-level languages
These languages are very close to the
human language (English-like) and they
can be read and understood even by
people who are not experts in
programming. These languages are
machine independent. This means that a
programmer concentrates on problem
solving during a programming session
rather than how a machine operates.
Types of low level languages
Machine language (First generation
languages).
Delphi Pascal
Visual Cobol
C++
Fifth generation languages (5GL’s)
These languages are designed around the
concept of solving problems by enabling
the computer to depict human like
intelligence. These programs are designed
to make the computer solve the problem
for the programmer rather than
programmer spending a lot of time to
come up with the solution.
Examples of 5GL’s
PROLOG
MERCURY
LISP
OCCAM
Object Oriented Programming Language (OOP)
Simula
Small talk
Web scripting languages
These languages are used to develop or add functionalities on
web pages. Web pages are hypertext documents created in a
language called HyperText Markup Language (HTML). The
languages consist of markup tags that tell the Internet browser
that the file contains HTML-code information and is
distinguished by a file extension of HTML. The markup tags
define the various components of a World Wide Web document
such as heading, tables, paragraphs, lists etc. HTML does not
have the declaration part and control structures, hence it’s not
considered as a true programming language. Due to its
simplicity, it has many limitations and cannot be used alone
when developing functional websites. Some special blocks of
codes called Scripts may be inserted in HTML pages using
scripting languages like JavaScript, VBScript etc in order to add
functionality to HTML pages.
Examples of HTML tags and their meanings
Tag Meaning
<HTML> Marks the beginning and end of a HTML document. All other tags and text fall
</HTML> between these two tags.
<HEAD></HEAD> Marks the header part of the document.
<TITLE> Gives title of te web page. Text between this tags appears in the title bar when the
</TITLE> page is browsed.
<BODY></BODY> Marks the body part of the document.
<H1> Sets size of text on the web page with H6 displaying the smallest and H1 the
</H1> largest size.
Creating a script using Javascript 1
Javascript is a popular scripting language.
Before writing your HTML program with a
script inserted, make sure that you have
the latest browser software installed on
your computer. Older browsers may not
have support for Javascript. If you are
using Internet Explorer, it should be
version 5.0 and above.
Creating a script using Javascript 2
Open Notepad and key in the following program. Do not
write the numbers to the left of each line of code.
1. <HTML>
2. <HEAD>
3. <TITLE>Scripting Example</TITLE>
4. </HEAD>
5. <BODY>
6. <HI><CENTER><B>we are the world</B></CENTER><HI>
7. <SCRIPT>LANGUAGE=‘JavaScript’>
8. document.write(‘My name is strongman’);
9. alert(‘congratulations for succeeding to run this script’);
10. </SCRIPT>
11. </BODY></HTML>
Creating a script using Javascript 3
After typing the entire
program, save your file
on the desktop as
Example.html and then
close the notepad. Notice
that the icon to your file
on the desktop look like
that of the default web
browser in your computer.
Problem definition
Program design
Program coding
Program testing
Program implemetation
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.
Sequence
Selection decision
Iteration (looping) repetition
Sequence
In this control structure, the computer
reads instructions from a program file
starting from the first top line and
proceeding downwards one by one to the
end. Hence sequential program execution
enables the computer to perform tasks
that are arranged consecutively one after
another in the code.
Example of how a sequential
programs code execute
The
program file
reader
reads
Begin {Procedure name} sequentially
Action 1 statement
Action 2 by
Action n statements
End {Procedure name} to the end
of the file
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.
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
Grade = “A”
Statement
ELSE
ELSE If Marks = 60 THEN
IF <Condition> Grade = “B”
Then ELSE
Statement If marks = 50 THEN
ELSE Grade = “D”
IF Endif
<Condition>Then Endif
Statement Endif
Endif Endif
End
Endif
End
CASE SELECTION
General format It’s an alternative to
CASE x of
Label 1 : Statement1 the nested IF. This
Label 2 : Statement2 selection is preferred
Label n : Statement n-1
ELSE to the Nested if in
Statement n order to reduce the
End case
many line s of codes.
Example:- Case selection can
CASE average OF only be expressed
80…100: Grade=“A”
70…79: Grade=“B” using integers and
60…69: Grade=“C” alphabetic characters
40…49:
ELSE
Grade=“E” only. The Boolean
Grade=“F” expression should be
End Case CASE integer OF or
CASW char OF
Iteration (looping) repetition
This is designed to execute the same
block of code again and again until a
certain condition is fulfilled. Iteration is
important in situations where the same
operation has to be carried out on a set of
data many times. There are three main
looping controls.
THE-WHILE-DO LOOP
REPEAT……….UNTIL LOOP
The FOR LOOP
THE-WHILE-DO LOOP
This repetitive structure tests the condition
first before executing the successful code
if and only if the Boolean expression
returns a true value.
Example
To withdraw money using an ATM a
customer must have a balance in his/her
account.
General format of THE-WHILE DO LOOP
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
2. A).
Inputs – initial amount borrowed
- Interest rate
- Number of years
Processing – the equation to calculate
monthly repayments.
Output – monthly repayments calculated
by the process
Answer of revision questions 3-3
2. B).
START
Input initial loan borrowed
Input interest rate
Input number of years
Calculate yearly
repayments
Monthly repayments
= yearly repayments/12
Display monthly
repayments
STOP