You are on page 1of 10

Algorithms and Flowcharting

Rev: 10/14/07 dld

Introduction
An algorithm is a set of steps for completing a task that can be written down and
implemented. Algorithms are made up of actions, or steps, decisions, inputs, and outputs.
For example, a cake recipe can be thought of as an algorithm for making cake. Such a
recipe describes inputs, actions, and outputs. It may also include decisions such as
whether or not to include nuts. Ideally, algorithms followed properly produce reliable
outcomes. For example, if a good cake recipe is followed properly, a good cake will
result. An algorithm prepared for a computer program explains how to take known inputs
and manipulate them into specific outputs by following a set of specific actions which
sometimes includes decisions.

Why use a flowchart? A flowchart is an efficient way to express algorithms in a succinct


but precise manner. Another benefit of flowcharts is that they can be used by both
technical and non-technical people. Flowcharts can be created that are implementation
independent. That is, they can describe physical or logical processes in such a way that
the logic is expressed independently of any particular programming language or
implementation method. This approach allows a person to ignore the details of any given
programming environment and to focus on specifying the logic of a process. Once the
flowchart is created, it greatly simplifies the process of implementing the flowchart into a
high-level computer language.

Flowcharts are used in a number of ways. Flowcharts are often created in the early stages
of formulating computer solutions where the process is being conceptualized. Flowcharts
are used as a way of creating efficient communication between business and technical
people. They may also be used to help train workers and users.

Symbols Used in Flowcharts


Flowcharts are made up of the standard symbols shown in Figure 1. The use of standard
symbols means that users do not need to be taught different sets of symbols.

Within a flowchart, actions (process steps), flow arrows, and decisions are fundamental
building blocks that can be combined in a variety of ways. Since these parts may be
combined in many ways, a flowchart can be used to define any process.

Copyright: Douglas L. Dean, 2007.


Symbol Explanation

The Start or End symbol begins and ends the


process.

The Input/Output symbol is used to represent any


type of input or output . Common types include
Input, Display (to screen), and Print (to paper )

The Decision symbol is a junction where a


decision must be made . A single entry may have
any number of alternative solution choices , but only
one can be chosen .

The Process symbol is used to represent any type


of function or action. This symbol may be used to
represent one step or a sequence of steps .

The Connector symbol is used when more than


one flow arrow head needs to come together .

The Flow symbol represents movement to the next


operation .

Figure 1. Standard Flowchart Symbols

The following are some guidelines for flowcharting:

Guidelines Description
A flowchart should be complete so that all necessary steps
Completeness
and decisions are included.
Clarity A flowchart should be clear and unambiguous
Flowcharts are drawn so that flow goes from top to bottom
Flow Direction
or left to right.
Single Process Exit Only one flow line should exit from a process symbol.
Flows Related to Only one flow line should enter a decision symbol, but two
Decisions flow lines should exit, one for each possible outcome.
Only one flow line should exit a start symbol. Only one flow
Single Start Flow
line should enter an end symbol.
Only one flow line should enter an end symbol. It is possible
Single End Flow to have multiple end symbols because multiple situations can
lead the program to terminate.
Table 1. Flowchart Guidelines

2
Logical Control Structures
The order in which process steps are done can be broken down into three fundamental
control patterns: sequence, selection, and repetition. With a sequential pattern, one step
is completed. Then a next step is completed and so on. With a selection pattern, the
answer to a decision causes a branch in the process. Thus, some steps are skipped under
certain circumstances. Decisions in the disciplined way we will use them have yes or no
answers. Thus one branch is taken if the decision is answered yes or true. The other
branch is taken if the decision is answered no or false. The repetition (also known as
loop) control structure, allows one or more actions to be repeated.

Sequence Selection

False True

Repetition (Loop )

True False

Figure 2. Fundamental Control Structures

3
A famous computer scientist, Edward Djisksta, proved that processes that can be mapped
to a computer program can be represented by one or a combination of these three control
structures.

Selection (Decision) Structures

Four common selection structures are now described. The IF…THEN structure shown in
Figure 3 is used when if a condition is true, then an action should be taken else
(otherwise) no action is taken. For example, if an employee needs training, then provide
training. Otherwise don’t provide training.

In contrast, as shown in Figure 3, the IF…THEN….ELSE structure does one thing if the
decision returns true and does something else if the decision returns false.

IF... THEN Structure IF... THEN ... ELSE Structure

False True False True


Need Hours >
Training ? 40?

Provide Regular Pay = OT_Hrs = Hours – 40


Training Hourly _Rate * Hours OT_Pay = OT_Hrs *
Hourly _Rate * 1.5

Figure 3. IF…THEN and IF…THEN…ELSE Structures

The Nested IF…THEN structure (Figure 4) is another common selection structure.


Depending on how the first decision is answered, the flow can go two ways. If the first
decision is true, flow is directed to another decision.

4
NESTED IF ...THEN Structure

False Hourly True


Employee?

False True
Regular Pay = Hours >
Monthly_Salary 40?

Regular Pay = OT_Hrs = Hours – 40


Hourly_Rate * Hours OT_Pay = OT_Hrs *
Hourly_Rate * 1.5

Figure 4. NESTED IF…THEN Structure.

With Multiway Selection (Figure 5), if decision 1 is true then sequence 1 is executed and
the multiway selection is finished. If decision 1 is false then decision 2 is tested, if this is
true, then sequence 2 is done and the multiway selection is finished. If decision 2 is false,
decision 3 is tested. If decision 3 is true then sequence 3 is done and the multiway
selection is finished. If decision 3 is false, the fourth sequence is done. Notice that the
number of paths equals the number of decisions plus 1.

5
MULTIWAY Structure

T F
Doughnuts
< 12?

T F
Sales = Doughnuts
Doughnuts * 0.60 <=24?
T F
Sales = Doughnuts
Doughnuts * 0.50 <= 48?

Sales = Sales =
Doughnuts * 0.45 Doughnuts * 0.40

Figure 5. MULTIWAY Structure

With a repetition control pattern, a step or set of steps is repeated until a certain
condition is met. The repeating code is sometimes referred to as a loop or iteration.

Two common repetition approaches are shown in Figure 6. The decision-first loop tests a
condition first. If the test returns true, then a process is executed. Then the flow loops
back to the decision again. If the test returns true, the process is done again. If the test
returns false, the algorithm exits the loop.

Decision-First (While) Loop. This is often referred to as a “While loop” because the
loop repeats while the decision returns true and terminates when the decision returns
false. Assume you are offered a bowl of cookies and are told you may eat as many as you
wish. You would decide whether you want the first cookie. If not, then you leave the
loop. If yes, you eat a cookie. Then the loop returns to the decision. If the outcome of the
decision is yes again you eat another cookie and loop back to the decision again. Thus
you might loop repeatedly until you have had all of the cookies you want. At that point,
the answer to the decision is false (or no) and you leave the loop.

Action-First (Repeat) Loop. With a repeat loop, the action is done at least once. Then
the decision is evaluated. If true, then the sequence loops and the action is done again.
This continues until the decision evaluates false. For example, consider a loading dock

6
where the only trucks that come are those that have room for at least some cargo. So
cargo is added to the truck. Then an evaluation is done to determine if there is more
room. If so, more cargo is added. When the decision is evaluated to false because of no
more room, the loop terminates.

Repetition (While Loop ) Repetition (Repeat Loop )


Decision First Action First

Want False Add Cargo


Cookie?

True

True Room for False


Eat Cookie More?

Figure 6. Repetition Loops

Putting All Together: Creating a Flowchart

To conceptualize a process in simple terms possible, it is helpful to visualize five


fundamental parts of any process: 1) start, 2) input the data; 3) perform the algorithm
(logic and calculations; 4) output the results, and 5) End. Then when you describe the
flowchart in detail, you can add the names of the specific inputs and outputs, as well as
describe the flows, actions, and decisions.

Figure 7 is a flowchart with a sequential flow structure that shows how to take the inputs
of an employ name, gross pay, and a tax rate and calculate tax and net pay. A variable
legend is used so that abbreviations can be used in the flowchart to substitute for longer
names in the flowchart. This keeps the flowchart compact and easy to follow. Notice that
short variable abbreviations can be used to create abbreviations that are easy to
remember. Use of abbreviations makes the flowchart easy to draw because it reduces the
volume of text that needs to be associated with flowchart symbols.

7
Flowchart A Flowchart B

Start Variable Legend Start

EN = Employee name
GP = Gross pay
Input TR = Tax rate Input
EN , GP , TR T = Tax EN , GP , TR
NP = N et pay
A = Answ er

Tax = GP * TR
Tax = GP * TR N P = GP - Tax
N P = GP - Tax

Print
Print (EN , GP , TR) ,
(EN , GP , TR) , T, N P
T, N P

Prompt “Process
End more payroll ?
Enter Y or N ”

Input A

Yes
A = “Y” ?

No

End

Figure 7. Calculate Net Wages Flowchart

8
Conventions for Inputs and Outputs
Although some processes will have one input and one output, some processes require
more than one input and output. Notice that in Figure 7 Flowchart A, more than one
variable is input to the process. Also, multiple outputs are produced.

It is not always necessary to prompt for input. For example in Figure 7 Flowchart A, all
inputs are obtained at the beginning of the process. In this case, no specific prompt
message is included in the flowchart to the user. In effect, the decision has been made to
abstracting away from specific text that makes up messages to the users. In this case, the
details of the prompts to users would be determined later when the program is created.

However, if a flowchart is being made such that it is later expected to be implemented in


an information system, and after the start of the process, additional input is required, the
creator of a flowchart should add a simple prompt so that it is clear what additional input
is required. For example, notice in Figure 7 Flowchart B that a loop has been added. In
this case, after net pay has been calculated for the first employee, the user is prompted to
enter input to signal whether the user would like to process net pay for another employee.
In this case, a short prompt “Process more payroll? Enter Y or N” is added to prompt the
user for input before the input symbol. Then the user enters a “Y” or “N” which is then
evaluated in the decision box. Such prompts are only required when input is required
after the start of a program.

Example of Prose that would be


Type Description in Output Symbol
Signals need for input that is required
after initial input is collected at the start
of the flowchart. Not needed if all input
Prompt Prompt “Enter Y or N”
is entered at the start of the flowchart.
Notice that the prose of the prompt
should be enclosed in quotation marks.
Used when the output is to be sent to
Display Display (EN, GP, TR) T, NP
the screen.
Used when the output is to be printed to
Print Print (EN, GP, TR) T, NP
paper
Table 2. Common types of Outputs

Notice than in Figure 7 Flowchart A, some inputs are also shown as output that have not
been changed (e.g., EN, GO, TR). In addition to these, outputs that have been calculated
in the flowchart are added, e.g., T, NP. Both are included in the output because it is
common practice to show both when running a program. For example, consider
implementing this flowchart in Microsoft Excel, where the user types in the Employee
name, gross pay, and the tax rate into specific cells in a spreadsheet. Then the program

9
would also calculate the amount of tax and net pay. In this case, the values that were
input would still be displayed on the screen. In other words, the fact that new outputs
were created does not mean there is not value is showing the inputs as well. A user would
print both the inputs that had not been changed as outputs along with the outputs
produced in the program for each employee. The convention we will follow is to show
outputs that were unchanged inputs in parentheses (EN, GP, TR). Outputs created in the
program are not enclosed in parentheses. The parentheses signify that the variables being
output are the same values as the variable input into the process. Failure to include
variables in parentheses signifies they are produced in the program rather than being the
same as those input by the user.

Assignments versus Equalities

It is important to note than in flowcharts and programs there are assignments to variables
in contrast to logical tests called equalities. Assignments and equalities are fundamentally
different things with different purposes.

An assignment to a variable is accomplished when an expression on the right side of an


equals sign is executed and the results are assigned to the variable(s) on the left of the
equals sign. For example, the assignment {Tax = GP * TR} is made in Figure 7 Flowchart
A. In this assignment, gross pay (GP) is multiplied by the tax rate (TR). The result of this
calculation is assigned to the Tax variable. In other words, an assignment is not a logical
comparison. Assignments always go from right to left. Said another way, the variable on
the left of the equals sign is always assigned a value based on what is done on the right
side of the equals sign.

Conversely, an equality is a logical test that is evaluated to return either a true or false in
terms of whether what is on the left side of the equals sign is the same as what is on the
right side of an equals sign. An example of this can be seen in Figure 7 Flowchart B.
After the user inputs a “Y” or “N” a logical equality is evaluated in the decision symbol.
If A = “Y”, then the decision returns true and the true flow line is followed. Otherwise
the program terminates.

Translating Narratives to Flowcharts


In this class, you will be provided with brief narratives that describe the logical content of
a process that you will map into a flowchart. Your task is to map the narrative to a
complete and unambiguous flowchart. This reading has been designed to provide the
tools to help you as you complete this process. When you become proficient at creating
flowcharts, much of the logical conceptualization required to later create a computer
program is completed. This makes it much easier to later map this logic into a program. If
the flowchart is not logically sound, then the program will suffer the same fate. So it is
good to develop skill in the proper and correct design of flowcharts.

10

You might also like