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

15. Programming Concepts

Programming involves creating a set of instructions, known as a program, which is written in a specific programming language to enable a computer to perform tasks. There are various programming languages categorized into low-level and high-level languages, and the process of translating these programs into machine code can be done using compilers, interpreters, or assemblers. Structured programming techniques, such as top-down and bottom-up design, help in building programs by breaking down problems into manageable modules, and tools like algorithms, pseudocode, and flowcharts aid in the development process.

Uploaded by

Oratile Aaron
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

15. Programming Concepts

Programming involves creating a set of instructions, known as a program, which is written in a specific programming language to enable a computer to perform tasks. There are various programming languages categorized into low-level and high-level languages, and the process of translating these programs into machine code can be done using compilers, interpreters, or assemblers. Structured programming techniques, such as top-down and bottom-up design, help in building programs by breaking down problems into manageable modules, and tools like algorithms, pseudocode, and flowcharts aid in the development process.

Uploaded by

Oratile Aaron
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

PROGRAMMING

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

Re-cap on Programming Languages

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.

All programming languages have:


A vocabulary, which is a set of keywords of the programming language.

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

Structured programming(Modularization) – Determines how to build programs based on


requirements. It facilitates the creation of programs with readable code and reusable components.
Structured programming uses a step-wise refinement where a big problem is broken down into
smaller problems (modules/sub-routines) so that it can be solved in smaller steps. The
programmer start with a set of general steps, go through a process in which these steps are
expanded (refined or broken into smaller steps) and the refinement continues until each step is a
program statement.

Advantages of Structured Programming/Step-Wise Refinement

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

WAYS OF BUILDING PROGRAMS (STRUCTURED PROGRAMMING TECHNIQUES)


1. TOP-DOWN DESIGN

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.

Steps of the Top-Down design

i. Identify the major function of a problem (main routine).


ii. Decompose (break down the main routine into smaller sections) the smaller sections called
sub routines because they are subordinates to the main routine.
iii. Analyse each subroutine (smaller sections) to determine if it can be decomposed further.
iv. Continue decomposing until each section performs a single action.

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.

ADVANTAGES OF THE TOP-DOWM/BOTTOM-UP DESIGN

 Simplicity of their design (Step by step).


 Reliable since the problem is solved fully to the last stage.
 Easy to read and maintain.
 Enable a program to be developed in stages.
 Modules/subroutine can be tested individually. (A module is a section of the broken down
program that performs a single function).
 Program modification is easier since changes can be made within specific modules.
 Allows large programs to be written (worked on) by several people.

TOOLS USED IN BUILDING PROGRAMS

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 has fixed number of steps.


 Each step in an algorithm specifies the action to be performed.
 The steps in an algorithm specify basic operations.
 An algorithm accepts input data.
 An algorithm generates one or more outputs after the input is processed.

An algorithm can be expressed as system flowchart, a program flowchart, data flow diagram or
pseudo-code.

3
2. Pseudocode

A pseudo-code is a method of describing a program in simple language which is closer to a


programming language, it uses control structures and keywords such as PRINT, IF, FOR,
WHILE, UNTIL, etc.
 Is a detailed yet easy description of what an algorithm must do.
 Is written in a formally-styled English language.
 Is used as an initial step in the process of developing a program.
 Provides a detailed template to programmers.
E.g.A pseudocode to enter two numbers and find out which one is the largest.

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

 Advantages of using pseudocode


 It is easier and faster to write.
 It does not need to be rewritten if any changes are made.
 It can be converted to a program by using any programming language.

 Disadvantages of using pseudocode

 It does not provide a graphical representation of an algorithm hence may be


challenging to be understood by programmers.

 A pseudocode showing too many nested conditions may be difficult to understand.

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.

Advantages of using a flow chart


It helps in analyzing the problems effectively.
It acts as a guide during the program development phase.
It help easy debugging of logical errors.

Disadvantages of using a flow chart


A lengthy flowchart may extend over multiple pages, which reduces readability.
Drawing a flowchart is time consuming.

5
The changes made to a single step may cause redrawing the entire flowchart.

RULES FOR DRAWING PROGRAM FLOWCHART

 Use standard flowchart symbols.


 Make only one start point and one end point.
 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.
 Use page connectors, if the flowcharts extend over multiple pages.
 Extra notes (annotations) should be written in the right hand margin of the paper.

SYMBOLS USED IN THE PROGRAM FLOW CHART

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

Obtain book Calculate Salary 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.

6
INPUT box OUTPUT box

Input a number Print a number

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.

On page connector – Off page connector –


1 1
is used to connect one is used to connect one
step in a flow chart to step in aflowchart to
another step on the another step on a
1 same page. 1 different page.

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.

SYSTEM FLOW CHART


A system flowchart is a diagram which gives an overall view of the system. It shows the tasks
that are performed on the data, such as sorting or updating.It also shows the type of media used
to hold data e.g. magnetic disk, tape etc. The flow of data is always indicated by arrows.
Symbols used in the SYSTEM Flow Chart

Decision Visual Display Unit

Process Keyboard Input

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

The following types of decision-making constructs can be used in pseudocode:

 if construct
 switch…case construct

The if construct

The decision-making construct if specifies a conditional expression that needs to be evaluated. A


decision is made based on the result of the conditional expression. The three main types of if
constructs are:

 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

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.

The switch…case construct can be represented in pseudocode, as shown in the following


pseudocode segment:
……
switch (expression)
begin
case constant 1:
//execute these statements
break
case constant 2:
//execute these statements
break
default:
//execute these statements
end

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

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

The repeat…until loop


In a repeat…until loop, the body of the loop is executed atleast once, regardless of the condition
being true or false.
 After executing the body of the loop for the first time, the condition is evaluated:
 If the condition is false, the body of the loop is executed again.
 If the condition is true, the control comes out of the loop.
13
Consider an example that calculates the sum of two numbers entered five times by the user.

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

You might also like