Professional Documents
Culture Documents
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.
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.
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.
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
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.
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.
4
NESTED IF ...THEN Structure
False True
Regular Pay = Hours >
Monthly_Salary 40?
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
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.
True
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
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
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.
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.
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.
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.
10