0% found this document useful (0 votes)
10 views

Programming Concerpts 2011

wilmat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Programming Concerpts 2011

wilmat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

PROGRAMMING CONCEPTS

A Program is a set of instructions that enable a computer to perform a task.

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.

 Low Level Languages


This is a computer programming language which is based on the machine code of the computer being
used.
• These are easily understood by the computer but very difficult for a programmer to
understand.
• These are machine-dependent i.e. a program written on one machine may not be usable on
another machine.
Low Level Languages
 Machine Language:
 This is the only type of language whose instruction codes can be executed by a computer. This comes

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.

High – Level Languages


 This is a type of language where the instructions are similar to English, which makes them easy for the
programmers to use. These languages are not machine-dependent which means that a program written
on one machine can be used on different computers with slight changes.
 Examples of such languages and their uses are:
 FORTRAN (FORmula TRANslation)-used in scientific applications.
 COBOL (COmmon Business Oriented Language) – for business applications
 JAVA – used for writing programs to be used on the internet.
 C++ - for commercial software. Very powerful in graphics.
 PASCAL – for general applications. Named after the French mathematician Blaise Pascal

Comparison of high and low level languages

High level languages Low level languages


 High level programs are easier to write  A low level program is efficient. If a high
 They are also easier to understand and to level program and a low level program are
modify written to do the same thing:
 High level programs are portable. A a) The low level program will run faster.
portable program is one which can be run b) When the two programs are translated
on a number of different types of computer into machine code the high level program will
with only minor changes. use more storage space than the low level one.

Low level programs make full use of the


computer’s capabilities.

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

Terms associated with programming


 Pseudocode: a language somewhere between spoken language and programming language used to
write algorithms. This ignores grammar.
 Algorithm: A sequence of steps followed in order to solve a problem written in pseudocode
 Debug: removing errors from a program
 Syntax error: an error that exists because the rules of the language have been broken. This type of
error can be detected by the translating program (compiler, interpreter, or assembler) and will cause
the program to stop execution.

 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.

 Rogue value/sentinel value: data used to terminate a loop.


 Dry run: Manual execution of every statement in the algorithm/program to determine whether it does
what it is supposed to do. This is done using a trace table with a column for each variable and rows for
each statement, to show the effect of each statement on variables

Structured Programming Techniques

 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)

PROGRAM FLOW CHARTS

• A program flowchart is a graphical representation of a routine (A routine is a set of step-by-step


instructions.)
• Program flowcharts show the sequence of operations carried out by a computer program.

An outline program flow chart diagram shows


❖ the start/end of the program
❖ The input/output of the operations
❖ How data is processed
❖ The main sections of the program
A detailed program flow chart shows all the operation carried out by the computer program step by
step.
The flow lines and arrows show the order in which the steps are to be carried out.

RULES FOR DRAWING PROGRAM FLOWCHART


• Use standard flowchart symbols
• Flow lines should never cross each other
• The usual direction of the flow in the flow chart is from left to right or top to bottom
• The arrows in the flow chart represents the direction of flow of control in the problem
• Extra notes should be written in the right hand margin of the paper.

9
SYMBOLS USED IN THE PROGRAM FLOW CHART

PROGRAM FLOW CHART SYMBOL


Start/Stop
Use as the first and last symbol in a in a
program

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

Used to show the flow/path of a sequence of symbols. Vertical without


arrow heads are assume to flow from top to bottom and horizontal line
without arrow heads assume to flow from left to right.

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.

Obtain book Calculate wages Make tea

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.

INPUT BOXES OUTPUT BOXES

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

You might also like