1
Rajarata University of Sri Lanka
Department of Computing
ICT1402
PRINCIPLES OF PROGRAM DESIGN &
PROGRAMMING
Lecture 04
Pseudocodes
K.A.S.H. Kulathilake
Ph.D., M. Phil., MCS, [Link]. (Hons) IT, SEDA(UK)
2
Objectives
• At the end of this lecture students should be able
to;
▫ Define and apply the rules of pseudocode.
▫ Convert a flowchart to pseudocode and vise-versa.
▫ Describe the advantages and disadvantages of
pseudocode.
▫ Describe the advantages and disadvantages of
flowcharting.
▫ Design programs in pseudocode that include
control structures and multiple modules.
3
Introduction
• Another formal program design tool.
• It was developed along with the structured
programming.
• Pseudocode works very well with the rules of
structured design and programming.
4
Rules for Pseudocode
• Write only one statement per line.
• Capitalize initial key word.
• Indent to show hierarchy.
• End multiline structures.
• Keep statements language independent.
5
Rules for Pseudocode (Cont…)
• Write only one statement per line
▫ In the task list, we have major tasks, sub tasks and
sub- sub tasks.
▫ We now convert each of those detail elements into
one line in our pseudocode.
▫ Each pseudocode line will represent one "action"
in our design or,will mark the boundaries of an
action.
6
Rules for Pseudocode (Cont…)
▫ Payroll example
We will assume our input includes name, the hours
worked, and the hourly rate and that we are to write
out the gross pay as well as the gross pay minus a
deduction of $15 for dues and uniforms.
▫ Task list
7
Rules for Pseudocode (Cont…)
▫ Pseudocode
▫ Notice that each statement is a single action and is
written on a separate line.
8
Rules for Pseudocode (Cont…)
• Capitalize initial keyword
▫ READ and WRITE are written in all capitals.
▫ They are "initial keywords" because they begin the
statement, and they are command words that give
special meaning to the operation.
▫ In pseudocode, we will use a limited set of
keywords, primarily READ,WRITE, IF, WHILE,
and UNTIL.
9
Rules for Pseudocode (Cont…)
• Indent to show hierarchy
▫ Indentation is one of the ways we use to show the
"boundaries" of the basic structures such as
conditions and loops.
▫ As long as we have only sequence structures, we
will not have indentation because we do not have
any questions of hierarchy.
10
Rules for Pseudocode (Cont…)
We have used
indentation to show
which statements are
within IF.
11
Rules for Pseudocode (Cont…)
• End multi-line structures
▫ Including end multi-line structure makes sure the
reader recognizes that we have finished with that
structure and are now going on to a new
statement.
▫ Apply at the end of selection and loop blocks.
12
Rules for Pseudocode (Cont…)
• Keep statements language independent
▫ You should not let your pseudocode turn into the
code that you will eventually write in.
As we know, there is not standardization of the
form to use in pseudocode. We will add to our
"rules" by developing a format for our design
structures.
13
Advantages and Disadvantages of
Pseudocode
• Advantages
▫ It can be done easily on a word processor, and
therefore
▫ It is easily modified.
▫ It implements structured design elements well.
• Disadvantages
▫ It is not visual: We do not get a picture of the design.
▫ There is no universal standard on the style or format,
so one company's pseudocode might look very
different form another's.
14
Advantages and Disadvantages of Flow
Charting
• Advantages
▫ It is standardized: all agree on acceptable symbols.
▫ It is visual.
• Disadvantage
▫ It is difficult to modify: a simple change may mean
redoing the entire flowchart.
▫ To do it on a computer, you need a special
flowchart package.
15
Storing and Accessing Data
• When we say READ name, hours worked, hourly rate, we
are saying that we want the computer to go out to some
external input device and retrive three values.
• These values will be stored in the computer's memory,
• Each memory location has an address, and we could use
that memory address as the way we refer to the value
when giving instructions to the computer.
▫ We could say, READ a numeric value, store it at address
61253.
▫ We could tell the computer to multiple the values at
addresses 61253 and 67952 and store the results of that
multiplication at address 71234.
• But as you can see, it would get very cumbersome very
soon.
16
Storing and Accessing Data (Cont…)
• The computer refers to the memory locations by address, but our
programming languages allow us to identify a name for the memory
locations.
• The value that us stored in memory is called a variable, and the name we
use to refer to it is the variable name.
• So when we say READ hours worked, we are really saying, "READ a value
and store it in a memory address that I will refer to as hours worked."
17
Storing and Accessing Data (Cont…)
• We have to be consistent with our variable
names so that there is no confusion when it is
time to convert the design to program code.
• Do not put the space between the words of the
variable names.
• It is better to use lowercase and capitals
consistently when declaring variables.
18
Storing and Accessing Data (Cont…)
• Assignment are also possible in calculations.
• Assignment always go from the right side of the
= sign to the left side.
19
Calculation Symbols
20
Design Structures in Pseudocode
• Sequence
▫ The sequence is just an ordered list of statements,
we implement it in pseudocode by giving an
ordered list of pseudocode statements.
• Selection
21
Design Structures in Pseudocode
(Cont…)
▫ The indent lines between the IF and the ELSE
from the "true" section- the statements to be
performed if the condition is true.
▫ The indented lines between the ELSE and the
ENDIF form the "false" section - the statements to
be performed if the condition is false.
▫ representing one-sided selection structures when
we only had statements to perform if the condition
was true.
22
Design Structures in Pseudocode
(Cont…)
▫ Special scenario with the condition section of a
selection.
▫ Assume that we want to write num3 if it is not
grater than num4
Our new structure
does the same thing
and is much easier to
understand.
23
Design Structures in Pseudocode
(Cont…)
▫ Relational conditional symbols are possible in
section construct
24
Design Structures in Pseudocode
(Cont…)
▫ Nested IF
25
Design Structures in Pseudocode
(Cont…)
▫ Try this
26
Design Structures in Pseudocode
(Cont…)
▫ Answer
27
Design Structures in Pseudocode
(Cont…)
• Loops / Iterations
▫ In pseudocode, we use the keyword WHILE to
introduce a while loop and the ENDWHILE to
mark the end of the loop.
28
Design Structures in Pseudocode
(Cont…)
▫ Calling a module We pass control to the module by using
the keyword DO, meaning "Go do the
module called Process and when you
finish that module, return to the
statement that follows."
Flowcharts have terminal symbols at each end
of a module.
Pseudocode usually starts each module with a
name but does nothing to indicate the end of a
module.
29
Design Structures in Pseudocode
(Cont…)
▫ Until loop
UNTIL loop tests the condition at
the END of the loop, and we exit the
loop when the condition is true.
With an UNTIL loop we are using
the keyword REPEAT to mark the
beginning of the loop body and
UNTIL to mark the end of the loop
body.
30
Example
31
Answer
32
Algorithm
• An algorithm is a procedure or formula for
solving a problem, based on conducting a
sequence of specified actions.
• A computer program can be viewed as an
elaborate algorithm.
• In mathematics and computer science, an
algorithm usually means a small procedure that
solves a recurrent problem.
33
Difference between Algorithm and
Pseudocode
Algorithm: Compute the area of rectangle
BEGIN
GET values for length and width
SET area to 0
calculate area by multiplying length and width
DISPLAY area
END
Pseudocode: Compute the area of rectangle
READ length, width
SET area = 0
area = length * width
print area
34
All Together
• An algorithm is a well-defined procedure that
allows a computer to solve a problem.
• A Pseudocode is a simple way of writing
programming code in English. Pseudocode is not
an actual programming language. It uses short
phrases to write code for programs before you
actually create it in a specific language.
• A program is a sequence of instructions in a
particular programming language, written to
perform a specified task on a computer.
35
Objectives - Revisit
▫ Define and apply the rules of pseudocode.
▫ Convert a flowchart to pseudocode and vise-versa.
▫ Describe the advantages and disadvantages of
pseudocode.
▫ Describe the advantages and disadvantages of
flowcharting.
▫ Design programs in pseudocode that include
control structures and multiple modules.
36
References
• Chapter 03 of Computer Program Design,
Elizabeth A. Dickson.
37
End of Program Design Module
Let’s start Programming Module in the next session