0% found this document useful (0 votes)
100 views36 pages

3b - Introduction To Flowcharts and Pseudocode

The document serves as an introduction to flowcharts and pseudocode, emphasizing their role in program design and structured programming. It explains the concepts of pseudocode, flowcharts, and their components, as well as the basic programming structures: sequence, selection, and repetition. Various activities and exercises are included to reinforce understanding and application of these concepts in practical programming scenarios.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views36 pages

3b - Introduction To Flowcharts and Pseudocode

The document serves as an introduction to flowcharts and pseudocode, emphasizing their role in program design and structured programming. It explains the concepts of pseudocode, flowcharts, and their components, as well as the basic programming structures: sequence, selection, and repetition. Various activities and exercises are included to reinforce understanding and application of these concepts in practical programming scenarios.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Introduction to

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

 If the steps in an algorithm simply follow each other in


succession, as in the example of opening the door
earlier on, then the simple sequence is an appropriate
building block.

 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

 If the steps in an algorithm repeat themselves one or


more times, then the repetition is an appropriate
building block.
 It is also referred to as the ‘WHILE LOOP’.
 It is presented as follows:
 WHILE ........ DO ....... ENDWHILE
The Repetition
The Repetition
 For practical purposes, they are two main variations of
the loop.
 The first variation is the one that we have just
encountered above, in which the decision point is
reached before the intermediate state. We already
know this as the WHILE-DO loop.
 In the second variation, the decision point is reached
after the intermediate state. It is also known as the
DO-WHILE loop. It is presented as follows:
 DO ........ WHILE .......
The Repetition
ACTIVITY 3: The Loop
 Suppose that the customer in our café has now been
served with black coffee which is what he preferred.
 Write code that should ensure that he drinks his coffee
sip by sip, until it is finished.
 Present your solution:
 first in a flowchart; then
 finally as pseudocode.
ACTIVITY 3 SOLUTION: The
Loop
 The flowchart has been excluded in this solution.
 A loop of some sort is the suitable solution in this
instance because the client should repeatedly sip on his
coffee until it is finished.
 The WHILE-DO is the best solution, since the DO-WHILE
would lead to absurd, unnatural actions.
 The solution that is expected form you should therefore
look something like this:
 WHILE there is some coffee in the mug DO
 Take a sip of coffee
 ENDWHILE
Top-down Programming

 With top down programming, here is the method for


developing programs:
 Start with a very short and simple statement of what
the program does, which is not the program name. For
example program chooses the share to be invested in
on the stock market.
 Go to the next level of detail for the whole program,
trying to describe the program as a sequence,
selection or repetition of main tasks. Each of these
tasks should be complete, but all the details will be
left for the next level.
Top-down Programming

 Keep repeating step two, until all the modules are


refined and no further steps can be undertaken.
 Convert each step into pseudocode or a flowchart that
represents the procedure.
 Check to make sure that each module performs the
correct actions on the appropriate data.
EXERCISES
 The following exercises are structured to help you to
fully comprehend the material that you have learned
in this lecture.
EXERCISE A
 Identify as many application areas as you can think of
in your field of study, either of:
 Finance; or
 Fiscal Studies
 where you think that there are opportunities to apply
the three main building blocks:
 simple sequence;
 simple selection; and
 Repetition.
EXERCISE A
 Examples to get you started are:
 The simple selection can be used in a program that helps
investor to decide whether to invest in equities or bonds.
 The simple sequence can be used in a program that directs
clients on the steps to follow in declaring imports at a port
of entry.
EXERCISE B
 An investor on the Zimbabwe stock exchange (ZSE) invests by
monitoring the price of a stock over time. She makes her
investment decisions based on what she thinks is going to
happen to the price of the stock in the following week.
 Whenever she thinks the price is going to rise in the following
week, she buys the stock in advance.
 When she thinks the price is going to fall, she sells in advance.
 Required
 The investor now wants to have a program that makes the
investment decisions for her.
 Create a flowchart and pseudocode for an investment program
that makes the investment decisions on behalf of the investor.
EXERCISE C
 XYZ Bank has offered John a loan of $1000. John is
expected to pay interest at 10% per year for the next 5
years, at the end of which the loan will mature. On the
date of maturity, John is expected to pay the last
interest instalment, and also to repay the loan
principal of $1000.
 Except for the advancement of the loan which is going
to happen at the beginning of the 5 year period, all the
other cash flows are expected to take place on the last
day at the end of each year.
EXERCISE C
 You wish to develop a program based on this loan case.
When the program is run, it should be able to display
the cash flows that have occurred each year until the
loan has matured by printing out the loan
advancement and repayment schedule.
 Required
i. Create a plan for implementing the loan scheme by
developing a flow chart diagram of the loan scheme.
ii. Turn the flow chart diagram into equivalent
pseudocode.

You might also like