Programming Concerpts 2011
Programming Concerpts 2011
For a computer to perform any specific task, instructions have to be fed in using a language that the
computer will understand. Since a computer understands only machine language (binary code) in form
of 0’s and 1’s any other language that will be used to feed instructions to the computer must eventually
be changed to Binary Code.\
A computer program is a set of detailed directions telling the computer exactly what to do, one step at a
time. A program can be as short as one line of code, or as long as several millions lines of codes.
Terms in Programming
Programming – writing down of instructions using a special language for the computer to follow to
perform a specific task
Programmer – The person who codes the programs from the information provided by the Systems
Analyst
Language Types
Different computer languages have been created with which to write these increasingly complex
computer programs. They can be categorized based on how close to normal speech they are, and thus how
far from the computer's internal language.
1
in binary codes i.e. 0’s and 1’s
Assembly language:
This language was developed to eliminate the need to use binary codes for each instruction in a
program. This language makes use of simple instructions (Mnemonics) like ADD for ‘add’, SUB for
‘subtract’, BEQ for ‘branch if equal’, etc and this one is use in preference to Binary codes because it is
easier to for the programmer to use and understand.
When a program is written in this language it has to be translated into machine language for the
computer to understand by software called an ASSEMBLER.
2
PROGRAM TRANSLATORS
These are programs that are used to convert a particular program commands into its machine code
format.
There are three types of program translators:
Assemblers
Compilers
Interpreters
Assembler
Program used to convert low-level language (assembly language) instructions into machine code.
Usually one assembly language instruction will correspond to one machine code instruction.
Compiler
This is a Program that translates the whole program written in a high-level language into machine code
all at once. This process of translating is known as compilation. During this process, the program
written in the high-level language (SOURCE CODE) is converted to an equivalent of the same
program in machine language (OBJECT CODE).
Interpreter
This is also another program that converts instructions written in a high level language into machine
code but one instruction at a time
Logical error: The type of error that causes the program not to solve the problem it was intended to
solve e.g. adding to instead of subtracting, multiplying wrong values. This type of error cannot be
detected during compilation but can be noticed during testing or very late after the program has long
3
been in use.
Structured programming technique is a methodology for programming that emphasizes the use of three
main program design concepts: modules, control structures, and single entry single exit. The use of
these concepts helps create programs that are easy to write, read, understand and debug (removing
errors in a program).
Single entry single exit means that all programs start at one point with the key word BEGIN and end
at one point with the key word END
There are mainly two techniques that can be used in structured programming:
1. TOP-DOWN DESIGN – The technique of breaking down a problem into the major tasks to be
performed; each of these tasks is then further broken down into separate subtasks, and so on until each
subtask is sufficiently simple to be written as a self-contained module
2. CONTROL STRUCTURES
There are three control structures that are used in structured programming
a) Sequence – In any program statements are written one after the other and they are executed in that
order
eg an algorithm for opening a locked door
Begin
Put key in lock
turn key
remove key
turn lock and push door open
End
b) Selection – Type of a structure where the next statement to be executed depends on the value of
4
an expression.
There are two types of the selection structure and these are:
-if…then…else statement
-case statement
In its simplest form, the If…Then…Else statement will have the general format below:
IF condition
Then
(first option)
Else
(Second option)
EndIf
When the condition is true, the first option will be taken and when the condition is false, the second
option will be taken. Only one option will be taken per run.
E.g Write an algorithm to determine whether a student’s given mark is a PASS or FAIL basing on the
fact that any mark less than 50 is FAIL and 50 upwards is pass
Begin
input mark
If mark < 50
Then
write ‘FAIL’
Else
write ‘PASS’
Endif
End.
c) Case Statement: This is a more general selection structure, which is more useful when a choice has
to be made from three or more possibilities.
Its general format is shown below:
CASE of
Statement 1
5
Statement2
………….
Statement x
END CASE
e.g. selecting a choice of fruit juices.
Begin
case drink
(lemon)
pour juice from lemon jug
(orange)
pour juice from Orange jug
(pineapple)
pour juice from Pineapple jug
endcase
End
ITERATION/REPETITION/LOOP
This is where a section of the program is repeated many times. There are basically two methods of
repeating statements:
Unconditional: where statement is repeated over a number of times from the start. This is archived by
using the FOR loop
Conditional: Where continuation or termination of the repetition is governed by what happens within
the loop. This is expressed by means of a Boolean expression (an expression which evaluates to true or
false). There are two popular types, the WHILE…DO statement and REPEAT…UNTIL.
a)For loop
Eg Write an algorithm to sum up a student’s 7 subjects scores and output the average.
Begin
set total to 0
For subject 1 to 7
Input subject_mark
6
Add subject_mark to total_mark
Endfor
Average: =total_mark/7
Output average
End
b)WHILE…DO loop
E.g. write an algorithm to sum up a student’s subjects scores and output the average.
Begin
set total to 0
set num to 0
While subject_mark<>99 do
Input subject_mark
Add subject_mark to total_mark
num=num+1
EndWhile
average:=total_mark/num
Output average
End
c)REPEAT…UNTIL loop
Eg Write an algorithm to sum up a students’ subjects scores and output the average.
Begin
set total to 0
set num to 0
Repeat
Input subject_mark
Add subject_mark to total_mark
num=num+1
until subject=99
average:=total_mark/num
Output average
End
7
SUMMARY OF STRUCTURED PROGRAMMING TECHNIQUES
1. while (condition)
{
statements;
}
end while;
2. for ( ; condition; )
{
statements;
}
End for;
3. do {
statements;
} while (condition);
4. If (condition) Then
(statements)
Else
(statements)
End If
5. if condition then
-- statements;
elseif condition then
-- more statements;
elseif condition then
-- more statements;
...
8
else condition then
-- other statements;
end if;
6. Repeat{
Statement
}
Until(condition)
9
SYMBOLS USED IN THE PROGRAM FLOW CHART
Process
Used to identify any kind of processing activity
Pre-defined process
Used to represent a process which as been set out
in details elsewhere
Decision
Used where a decision has to be made in selecting
the subsequent path to follow. There is only one
entry point and two exits
Input/Output
Used where data input and output is to be performed
Connector
links one page to another or one program to another
10
THE START OR STOP BOX
These symbols are used at the start and stop end of flow diagrams. There is only one stop or end box. The
line with the arrow going out or into of the box shows whether it is stop or start box. There is always a
one line of flow (line of arrow). The name of the box is placed inside the box.
START
STOP
PROCESS BOX
Process box have two arrows, one going in and the other going out. Sometimes they are called activity
boxes, because something is done. A progress is the something which is done with the information. Quite
often it will involve a calculation, but it could be just doing something e.g. hitting nail or paying a bill.
Anything you have to do you put it in a process box.
INPUT/OUTPUT BOXES
Input is what is required for the process to be done. If a calculation needs to be done, then the input will
be some figure. If you are writing about a job your input would be a job advertisement. The input/output
boxes have an arrow going in and coming out.
Write job
Read application
advertisement 11
Input a Print a number
number
DECISION BOXES
These boxes are used to ask questions to which there are two answers: yes and no. The answer will
always be yes or no- never ‘I don’t know’. Yes and No must be written by the path to show which part is
being taken. One flow lines goes into the decision box and two lines leaves it once the decision inside the
box has been made. The question inside the box must be one that the answer will be either yes or no. The
decision boxes one of the flow line can come out of the box to either the left or the right. This is useful in
flowcharts in which there are a lot of decision boxes, because it stops the flowchart from becoming
clustered.
yes
No
CONNECTOR BOXES
These circular boxes are used to connect the flow lines when you have to turn over a page. They are
usually numbered in case there is more than one drawing flow diagrams, it is best to start at the top of a
clean page in order to avoid confusion. Connectors are useful in flow diagrams where there are lots of
loops, because they make the diagram looks neater.
2
2
12
DRY RUN
Once you have drawn a flow chart you need to check it for logical errors. This done by performing what
is called a dry run. A dry run is a check to make sure that the flowchart is correct. Test data is put into the
flowchart and each instruction in the boxes is obeyed in turn. When the data has been processed by the
flowchart, we make sure that the output is correct. If it is not correct, then a mistake has been made in the
drawing of the flowchart.
13