1338792238 introduction to comp programming
1338792238 introduction to comp programming
COMPUTER
PROGRAMMING
Maxwell Okumu
Computer programming
Computer Programming is defined as the process
of creating computer software using a
programming Language. Computer programs are
written by Human
individuals(Programmers)
A computer program is a step by step set of
instructions that a computer has to work through
in a logical sequence in order to carry out a
particular task. The computer executes these
instructions (obeys the instructions) when told to
do so by the user.
Programming Languages
Programming languages are the vocabulary and
set of grammatical rules for instructing a computer
to perform specific tasks. There are many different
types of programming languages each having a
unique set of keywords (words that it
understands) and a special syntax (grammar)for
organising program
instructions.
Syntax
Syntax refers to the spelling and grammar of a
programming language. Each program defines its
own syntactical rules that control which words the
computer understands, which combinations of
words are meaningful, and what punctuation is
necessary. Text-based programming languages are
based on sequences of characters, while visual
programming languages are based on the spatial
layout and connections between symbols (which
may be textual or graphical).
Example of a program code
PYTHON)
# Define a function to convert temperature from Fahrenheit to Celsius
def fahrenheit_to_celsius(temp_f):
# Formula to convert temperature
temp_c = (temp_f - 32) * (5/9)
# Return the result
return temp_c
Explanation:
The fahrenheit_to_celsius function takes a temperature in Fahrenheit
the user, which is then converted to a float using the float function.
The converted temperature in Celsius is then printed using the print
function, with a formatted string to show 2 decimal places.
There are many computer programming languages
and so the programmer will have to decide which
one to use for solving a particular problem. These
languages must be learnt just as Swahili, English or
French etc.
Levels of programming languages
Programming languages are classified into five
major categories: machine languages (first
generation languages), assembly languages
(second generation languages), third generation
languages, fourth generation languages, and
natural languages. Machine and assembly
languages are referred to as low level languages;
third generation, fourth generation and natural
languages are categorised as high level languages.
A low level language is machine dependent; that
is, it is written to run on one particular computer. A
high level language is machine independent, which
means the high level language code can run on
many different types of computer.
There are two types of low-level programming
languages: Machine Language and Assembly
Language.
Machine language – First
Generation Language
(1GL)
The machine language writes programs using the
machine code of 1s and 0s, which is directly
understood by the computer.
The main problems with using machine code
directly are that it is very easy to make a mistake,
and very hard to find it once you realise the
mistake has been made.
Characteristics of
1GL to execute because it is already in the language
Fastest
that the computer can understand
Difficult to interpret (requires the aid of a reference
manual to interpret the meaning of each code)
Easy to make mistakes in the sequence of 1s and 0s;
replacing a 1 for a 0 can result in the wrong
command/instruction being
executed
It is difficult to identify mistakes
made
Time-consuming and tedious to write
Machine dependent
Programing becomes more difficult as the complexity
Assembly language – Second
Generation Language
(2GL)
Assembly language is written using mnemonic codes
(abbreviated English words) or short codes that
suggest their meaning and are therefore easier to
remember. These codes represent operations,
addresses that relate to main memory, and storage
registers of the computer. Typical codes might be:
LDA, STO, ADD, NOP, etc.
An example of a program code to add and store two
numbers would be:
LDA A, 20 : load accumulator A with the value 20
ADD A, 10 : add the value 10 to accumulator A
STO B, A : store contents of accumulator A into
storage register B
Fire truck
Delivery
Truck
Dump
truck
Major high level programming
languages used
FORTRAN (FOTmula TRAnslator)developed in the
late 1950s developed to design scientific
application
sCOBOL (Common Business Oriented Language)
developed in early 1960s to develop business
applications
.RPG (Report Program generator) was developed in
early 1960s to assist in generating reports and
complex
calculations.
BASIC (Beginner’s All-purpose symbolic instruction
code) developed in mid 1960 Basic versions include
MS-BASIC, QBASIC, SmallBASIC and visual basic.
Pascal was developed in the late 1960s for the
purpose of teaching structured programming
concepts
C developed in the early 1970s to write system
softwar
eAda was developed in the late 1970s originally
developed to meet the needs of embedded
computer systems
C++ developed in the 1980s is an object-oriented
language mainly to develop application
software
Note that in addition to the major languages
discussed above, there are many other
programming languages that have been developed
such as JavaScript, and Python
Language Translators
Language translators are system programs that
convert assembly language and high-level
language into the machine language.
The computer does not understand assembly
languages or high-level languages. Computers
work in machine code or machine language.
Source code
Source code is a program code of either an
assembly language or high-level language that the
programmers write in a program editor. Source
code cannot be understood by the computer until
it has been translated into machine code.
Object code
Object code is a program code in machine-
readable form (a source program that has been
translated).
Assemblers
The assembly-language program must be
translated into machine code by a separate
program called an assembler. The assembler
program recognises the character strings that
make up the symbolic names of the various
machine operations, and substitutes the required
machine code for each instruction.
Compilers
Compilers are language translators that translate
high-level languages into machine code.
A compiler translates the entire program (source
code) to machine code, and then the code is
executed. The translated codes are known as object
codes and are saved as a file on disk. The object code
(machine code) stored on disk has an EXE file name
extension. It is then loaded or linked (stored in
memory) and executed
How a Compiler works
Interpreters
Interpreters are language translators that translate
high-level language into machine code. An
interpreter translates and executes one instruction
at a time as it is encountered. The machine codes
are not saved after
execution
How an Interpreter works
The program development life
cycle (PDLC)
During the development of a program there are
several stages. These can be described by the
Program Development Life Cycle (PDLS)which
has several steps which lead to the solution of
the problem.
PDLC is the continuous process that consists of
five/six general steps:
1. Analyse problem
2. Design program
3. Code program
4. Test program
5. Formalise program
6. Maintain program
PDLC
Problem analysis
This involves problem recognition, which involves
identifying the need to overcome a given problem
for example, the need for a school to process
students marks and grade accurately at the end of
term and problem definition, which involves
identifying the users program objectives, desired
inputs, outputs, and processing.
Defining the
problem
In defining the problem there must be no ambiguity. The
problem should be clear and concise and have only one
meaning.
Examples of unambiguous problems are:
i) Calculating the price of an item after a 10%
discount
ii) Converting a temperature from ° C to ° F
iii) Computing the average rainfall for the month of May
in a certain place.
Analysing the problem
Process exams
Begin
ENTER TWO
NUMBERS;A,B
CALCULATE SUM=A+B
AVERAGE = SUM/2
AVERAGE
GRADE = “B”
>=60?
GRADE =“A”
PRINT
SUM,AVERAGE
AND GRADE
Flowchart (input-
output)
Lamp
flowchart
Structured program designs
When designing a program, you must reason through
the problem to determine the correct logic. The logic
of a program can be established through the use of
control structures. Structure design involves
identifying the logical order of the procedure required
to accomplish the task described in a given module.
This procedure is what is known as the solution
algorithm or program logic.
Control structures or logic structures/constructs
control the logic sequence in which computer program
instructions are executed.
Four basic control structures are used to form the
logic of a program in structured program design:
sequence, selection, case and iteration
(loop).
The Sequence control structure
The sequence structure is used to show a single
action or one action followed sequentially by
another e.g. reading a record, followed by
calculating totals and averages and then printing
the averages.
Sequence control structure
Action 1
(Read students
record)
Action 2
(calculate Total)
Action 3
(Calculate average)
Action 4
(rank students)
The Selection control structure
then-else)
(if-
Selection is a process that allows a computer program
to compare values and then decide what course of
action to take based on a certain
condition.
The selection structure therefore is used to tell the
program which action to take next, based on a certain
condition; when the condition is evaluated, its result is
either true or false : if the result of the condition is true,
then one action is performed; if the result of the
condition is false, a different or no action is performed.
For example, a condition that if the average mark
is 50 and above, the student is passed, or else, the
student fails. i.e. if a condition is true, then do one
thing, else do another.
Because of the above, the selection structure is
also known as the if-then-else control
structure.
IF score >= 60 THEN
Comment = "Pass"
ELSE
Comment = "Fail"
ENDIF
PRINT comment
Selection control structure
flowchar
t
Test No
ELSE statement
condition
Yes
THEN statement
If ..then.. else
If(age <30)Then
TextWindow.WriteLine("too young ")
Else
TextWindow.WriteLine(“too old ")
endif
If (Clock.Hour < 12) Then
TextWindow.WriteLine("Good Morning
World")
EndIf
If (Clock.Hour >= 12) Then
TextWindow.WriteLine("Good Evening
World")
EndIf
Case control structure
The case control structure is a variation of the
selection structure that involves more than a single
yes-or-no decision. The case structure allows
several alternatives or cases to be presented. The
case control structure saves the programmer the
trouble of having to indicate a lot of separate IF…
THEN…ELSE
conditions.
Example of an IF…THEN…ELSE structure involving several alternatives:
FOR Counter = 1 TO 4 DO
<Action to be
repeated >
ENDFOR
When this statement is executed, the counter
variable is initially set to the beginning value, in
this case, 1. After the execution of the instructions
between the FOR and the ENDFOR, the counter
variable is increased by 1. The instructions are
repeated and the counter variable increases until it
becomes equal to the ending value, in this case, 4.
So the instructions are repeated four
Using Small Basic
TextWindow.Title = “wellcome Program"
TextWindow.WriteLine("Welcome to Beginning
Small Basic!")
A program is made up of many statements. Every
line is a statement and every statement instructs
the computer to do something:
TextWindow is an object built into Small
Basic – it is the window that displays the
output of the program. Objects have
both properties and methods. Properties
describe objects, while methods do things to
objects. In first single line of code, we are
setting the Title property of
the TextWindow object to the text string
“Welcome Program”. The dot (.) and
assignment operator (=) are punctuations that
must be placed appropriately for the computer
to understand your intent.
This line of code literally says “set
the Title property of the TextWindow object
to Welcome Program.”
TextWindow.WriteLine("Welcome to Beginning
Small
Basic!")
This line of code uses the
TextWindow WriteLine method to perform
the task. We say the text “Welcome to
Beginning Small Basic!” is passed to
the WriteLine method
The input is placed in parentheses – which
then results in the input text being written in
the text window.
Some basic rules of Small Basic
All keywords must be spelled correctly.
To set an object property, we use the
following ‘dot’ convention:
ObjectName.PropertyName =
PropertyValue
ObjectName.MethodName(MethodInputs
)