3b - Introduction To Flowcharts and Pseudocode
3b - Introduction To Flowcharts and Pseudocode
Flowcharts and
Pseudocode
CFI 1202
Introduction
The lecture introduces the use of flowcharts and
pseudocode as tools for designing programs.
You should regard this lecture as one to be read and
reread several times as part of learning practical
programming skills.
Do not confuse pseudocode to be some kind of a
programming language. It is not! It is just a universal
way to communicate program plans that are intended
to be converted to a programming language at a later
stage.
Flowcharts and Pseudocode
The term pseudocode has been referred to several
times before in this lecture. It is now time to go into
details.
It is necessary to start with general examples then
move to examples that are more relevant to the field
of study.
In this module, a special type of font
(Lucida console) it used on the code
itself to make the pseudocode appear,
more like it would on a computer screen.
Pseudocode keywords are in bold type.
What is Pseudocode?
Pseudocode is a representation of programming building
blocks in words (in the same manner that flowcharts
represent programming building blocks diagrammatically).
It is important to note that, even though pseudocode uses
English words (just like programming languages like Python
and Delphi do), pseudocode is not itself a programming
language.
You will therefore be well advised not to take program
structures from a programming language and think that they
are pseudocode.
While pseudocode can be directly and easily converted into
any programming language of your choice, the opposite is
not always true.
ACTIVITY 1: Pseudocode procedure
for opening a locked door
Write the pseudocode procedure for opening closed
door.
ACTIVITY 1 SOLUTION: Pseudocode
procedure for opening a locked door
BEGIN
Put Key in Lock
Turn Key
Remove Key
Turn Handle and Push Door Open
END
Even in this simple example, certain assumptions have
been made, such as that the door is opened by pushing
and not by pulling.
Structured Programming
The term structured programming was first used by Professor
Dijkstra in the 1960’s. Computer manufacturers tried his
methods and achieved considerable success.
Unfortunately, as soon as the word ‘structured’ is placed in front
of the word ‘programming’, it appears as if it is something more
complicated.
In fact it is the opposite. Structured programming makes
programming easier through the use of tried and tested building
blocks. According to Professor Dijkstra, all programs could be
written in only four basic structures; sequence, branch, loop and
modules.
Many programmers today agree that the pseudocode for any
program can be written in its entirety just using the first three
basic forms, which are discussed now.
Other forms do exist of course, but they will be added on later.
Flowcharts and State Diagrams
For our purposes we will use the terms flowchart and
state diagram interchangeably, to mean essentially the
same thing.
You should however, remain mindful of the fact that
what are referred to as flowcharts and state diagrams
have subtle differences in strict software development
terms, although they are in many respects related.
Flowcharts have general applications in other areas
outside of modelling, while state diagrams have been
adapted especially for modelling under what is
referred to as Unified Modelling Language (UML).
The Components of Flowcharts
Begin state
Every program begins somewhere
End state
Every program ends somewhere
Intermediate state
When running, every program is either in one
state or another
The Components of Flowcharts
Transition
Every program moves from one state to
another
Decision point
Every program makes decisions, unless it is
made up of just a simple sequence.
Input/Output point
The point in the program where it receives
input, or gives output
The Rules for Building Blocks
Programs have a unique (single) entry point (begin
state) and a unique exit point (end state).
If you design a building block that does not have
unique entry and exit points, it will be virtually
unimplementable in a program (how will your program
decide which route to use to begin or to end?).
Every begin state should have an associated end state
(otherwise you will have just designed a program that
will not terminate).
Therefore a program should always begin somewhere
and end somewhere.
Flow Charts Layout
Well-presented flow charts should generally start at the
top of the page and end at the bottom of the page.
If they is any drift in the flow from one side of the page
to another, then the drift should be from the left side
of the page, to the right side.
Although these are not strict rules, it is generally
expected that the begin state is somewhere around the
top left side, and the end state somewhere around the
bottom right side.
The Simple Sequence
It is presented as follows:
BEGIN ........... END
The Simple Sequence
The Simple Selection
If the steps in an algorithm involve selecting between
two options, then the simple selection is an
appropriate building block.
It is also called the ‘IF-THEN-ELSE’ selection for a good
reason.
It is presented as follows:
IF ........ THEN ........ ELSE ....... ENDIF
The Simple Selection
The Simple Selection
A simple sequence stream can only be split into more
than a single stream through a simple selection.
In other words, without a reaching a decision point, a
sequence stream cannot be split.
CLASS ACTIVITY 1: The Simple
Selection
Suppose a customer in a Cafe has to make a choice
between tea and coffee.
How would the pseudo code look like?
Activity 1 Solution: The Simple
Selection
BEGIN
IF
Preferred Drink = Tea
THEN
Pour tea into tea cup from pot marked teapot
ELSE
Pourcoffee into coffee mug from pot marked
coffeepot
ENDIF
END
Activity 1 Solution: The Simple
Selection
Once you are comfortable writing pseudocode, the
condition of the IF statement may be written is a single line
as follows:
BEGIN
IF Preferred Drink = Tea THEN
Pour tea into tea cup from pot marked teapot
ELSE
Pour coffee into coffee mug from pot marked
coffeepot
ENDIF
END
ACTIVITY 2: The Simple Selection
Of course the choices made by the customer could be
more complicated than this.
For example, the customer may need to make choices
between:
tea and coffee,
taking their drink black or with milk,
taking the drink with sugar or without.
This could be represented in a decision tree, and then
finally be turned into pseudocode.
Try it and see how the decision tree and the
pseudocode will look like.
ACTIVITY 2 SOLUTION: The Simple
Selection
No solution is provided for this Activity in this lecture.
Solving this Activity is left to you as an assignment for
practice purposes.
The Repetition