15. Programming Concepts
15. Programming Concepts
A computer needs a set of instructions called a program to perform the I-P-O cycle.
The process of developing these instructions is called programming.
Programs are written by computer experts known as programmers.
A program needs to be written in a specific language called programming
language, so that computer can understand the instructions.
The set of instructions required by a computer specifies:
i. The kind of input that will be provided. For example, in the case of Flight reservation
application, details about flight date, flight time, class, and destination will be some of the inputs.
ii. The processing that needs to be done.For example, in the case of reservation application,
accepting the values, checking for availability of seats, and displaying results will be some
processing.
iii.The kind of output expected.For example, in the case of reservation application, the seat
availability status can be an expected output.
A programming language helps the computer understand the instructions.
Instructions in a program can be:
Sequential
Decision-making
Iterative/Repetition
A computer uses a low level language which consists of a set of instructions which are easily
understood by a computer and difficult for a programmer to understand. Therefore, any
instruction given to a computer must be a sequence of 0s and 1s (machine language).
Examples of low level languages are; Machine language (A code of 0s and 1s which is directly
understood by a computer) and Assembly language (Uses simple instructions such as ADD,
SUB, etc.)You need an assembler to translate assembly language to machine language.
A high-level programming language consists of a set of instructions which are easily
understood by a programmer and difficult for a computer to understand. It is represented by
using simple English words.
Examples of high-level programming languages are; C, C++, Java, HTML, BASIC, COBOL,
FORTRAN, LOGO.
1
A grammar, which is referred to as the syntax.
TRANSLATING PROGRAMS
Translators are system software used to convert the program / commands into machine code.
There are three ways of translation programs using the following software;
Compliers
Interpreters
Assemblers
Interpreter – Converts instructions (input) into machine code and carries it out.
Compiler – Converts high level language (instructions) into machine code.
Assembler– Translate/converts assembly language to machine language.
STRUCTURED PROGRAMMING
A big problem is broken down into smaller problems, hence easily solved.
Each step can be tackled by a different person, hence allow team work.
The top-down approach is a type of step-wise refinement method in which the solution of the
problem starts at the top and works its way down to the bottom until all the section of the
problem is solved.
2
2. BOTTOM-UP DESIGN
With the bottom-up design the solution to the problem starts at the bottom and work its way up
until the big problem is solved.
Before writing a program, we need to derive a procedure to solve the problem. These can be
represented by using the following tools:
Algorithm
Pseudocode
Flowchart (Program Flow Chart or System Flow Chart)
1. Algorithm
An algorithm is a set of instructions or steps outlining how a specific process is undertaken (Eg.
Withdrawing money from an ATM). An algorithm has the following characteristics;
An algorithm can be expressed as system flowchart, a program flowchart, data flow diagram or
pseudo-code.
3
2. Pseudocode
1. Begin
2. Prompt the user to input two numbers; Num1 and Num2
3. If (Num1>Num2) Then print (“Num1 is greater than Num2”)
4. Else
5. Print (“Num2 is greater than Num1”)
6. End If
7. End
3. Flow Chart
A flowchart is a graphical representation of an algorithm. It consists of a set of symbols.
Each symbol represents a specific kind of activity depicted/represented in the algorithm.
Program flowcharts show the sequence of operations carried out by a computer program. An
outline program flow chart diagram shows;
4
The start (begin) and stop (end) of the program
The input and output of the operations
How data is processed (processes)
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.
Example of a program flow Example of a system flow chart for
chart for adding 2 numbers adding 2 numbers, display the sum,
and display the sum. store the sum and print the sum.
5
The changes made to a single step may cause redrawing the entire flowchart.
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 one line of flow (line of arrow exiting the Start and entering the Stop).
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.
6
INPUT box OUTPUT box
DECISION box
These boxes are used to ask questions to which there are two answers: Yes andNo. The answer
will always be either a Yes or No, therefore the words 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.
No
Yes
Connector box
This is used for connecting the flow lines when you have to turn over the page. They are usually
numbered in case there is more than one drawing of the flow chart.
ANNOTATION box
This is used to insert comments in a flow chart. Comments can be inserted in flow charts to
explain the steps.
You write your
comment here
C
7 o
m
e
n
Example of a program flow chart for a
program that reads a student number and
searches for the student record using the student
number. It then outputs the student’s record.
Printouts Disk/Storage
8
Example of a system flow chart for
a program that add 2 numbers,
display the sum, store the sum and
print the sum.
PROGRAMMING CONSTRUCTS
Computers scientists have defined three constructs for structured program or algorithm. The idea
is that a program must be made up of a combination of any of these three constructs;
Sequence
Selection (decision)
Iteration (repetition) (loop)
1. SEQUENCE
An algorithm and eventually a program is a sequence of instructions, which can be simple
instruction or either a combination of the other two constructs.
Sample:
Action 1
Do action 1
Do action 2
Action 2
………………..
………………..
Action n
Do action n
9
2. SELECTION (Decision)
Some problem cannot be solved with only a sequence of simple instructions. Sometimes you
need to test a condition. If the result is true you follow a sequence of instructions; if it is false,
you follow another sequence of instructions.
Sample:
if a condition is true
Then
Do series of action
Else
Do another series of actions
end if condition
if construct
switch…case construct
The if construct
Simple if construct
if…else construct
Nested if…else construct
In simple if construct, if the condition specified is true, the statements contained within the if
block are executed.
if <condition>
begin
<statements to be executed if condition
is true>
end
In the if…else construct:If the condition is true, the statements contained within the if block
are executed. If the condition is false, the statements within the else block are executed.
if <condition>
begin
<statements to be executed if condition is
true>
end
else
begin
<statements to be executed if condition is
false>
10
The nested if construct: At times, the pseudocode needs to be evaluated based on the
condition within another condition. In that case, you can use an if…else statement within
another if…else statement. This construct is known as nested if…else construct. There is no
limit or rule to specify the number of nested decision statements.
The switch…case construct:Is used when a variable needs to be compared with multiple values.
This further enables you to make a decision. It consists of one switch statement, a number of
case statements and a default statement.
3. ITERATION (Repetition)
One of the most important characteristics of a program is to execute a series of instructions
repeatedly.This characteristic of a program helps you to control the number of times a task is to
be repeated.
A loop is a sequence of instructions that will be repeated more than once.
11
A loop must always perform certain steps in a specified sequence.
The following types of loops can be used in pseudocode/flowchart to perform the
repeated tasks:
for loop
while loop
repeat…until loop
The for loop
The for loop construct provides a compact way of specifying the statements that control the
repetition of the steps within the loop.
The for loop consists of the following three parts: - Initialization expression
- Test expression
- Increment/decrement expression
Consider an example that accepts 5 numbers and checks whether the number is even or odd. If
the number is even, display the message, ‘The Number Is Even’. Otherwise, display the message,
‘The Number Is Odd’.
begin
numeric nNum1, nCtr
for (nCtr = 0;nCtr<= 4; nCtr = nCtr + 1)
begin
display ‘Enter a number’
accept nNum1
if (nNum1 ÷2 == 0)
begin
display ‘The Number Is Even’
end
else
begin
display ‘The Number Is Odd’
end
end
end
The while loop executes till the condition is true; and the moment condition becomes false, the
loop breaks.
12
The while loop construct can be used for both fixed and variable loops.
The while loop is an iterative construct with a test expression at the top of the iterative set of
statements.
The following pseudocode segment shows the syntax of the while loop:
…
while (condition)
begin
//Body of the while loop
end
…
Consider the example that accepts 5 numbers and checks whether the number is even or odd. If
the number is even, display the message, ‘The Number Is Even’. Otherwise, display the message,
‘The Number Is Odd’.
begin
numeric nNum1, nCtr
nCtr = 0
while (nCtr<= 4)
begin
display ‘Enter a number’
accept nNum1
if (nNum1 % 2 == 0)
begin
display ‘The Number Is Even’
end
else
begin
display ‘The Number Is Odd’
end
nCtr = nCtr + 1
end
end
begin
numeric nNum1, nNum2, nSum, nCounter = 0
repeat
begin
display ‘Enter the first number’
accept nNum1
display ‘Enter the second number’
accept nNum2
nSum = nNum1 + nNum2
displaynSum
nCounter = nCounter + 1
enduntil (cCounter == 5)
end
DRY RUNNING A PROGRAM
The concept of a dry run can be used to perform a logic check (correctness) and understand
the flow of control. It provides a step-by-step evaluation of values in a program.
Dry run:
Helps you perform a logic check.
Helps you understand the flow of control in a flowchart or in a pseudocode representation.
Evaluates the output of a program with a set of sample values.
Provides a step-by-step evaluation of values in a program.
Example: A flowchart and write the pseudocode to accept 5 numbers and display the total
number of odd and even numbers.
14
15