Documente Academic
Documente Profesional
Documente Cultură
What is an algorithm? 3
Features of a good algorithm 3
Expressing algorithms 5
Summary 29
437531326.doc
© State of New South Wales, Department of Education and Training 2006 1
Reading: Identify algorithm structures
What is an algorithm?
437531326.doc
© State of New South Wales, Department of Education and Training 2006 2
Reading: Identify algorithm structures
An algorithm might only specify the essential features of a solution, and not
the details of error handling. It will then be up to the programmer to enhance
the algorithm accordingly.
There are some special cases where a program is purposely designed not to
end, typically where the program is controlling an external device or system
(for example a mobile phone, or an aircraft flight-control system).
437531326.doc
© State of New South Wales, Department of Education and Training 2006 3
Reading: Identify algorithm structures
Expressing algorithms
Described below are three common ways to express the structure of an
algorithm:
1 Pseudocode
2 Flowcharts
3 Nassi-Schneidermann diagrams.
Pseudocode
Pseudocode is structured English used for describing algorithms. The
purpose of pseudocode is to provide a means of specifying algorithms that is
independent of any particular programming language. It allows the program
designer to focus on the logic of the algorithm without the distraction of
programming language rules.
There is a wide variation in pseudocode syntax, but the main thing is that
the meaning is readily understood and unambiguous. Organisations that use
pseudocode often develop an in-house standard which defines the structure,
constructs and keywords that may be used by their designers.
437531326.doc
© State of New South Wales, Department of Education and Training 2006 4
Reading: Identify algorithm structures
Wake Up
Perform morning things
IF it is raining THEN
Watch TV
ELSE
Play golf
ENDIF
Get ready for lunch
Flowcharts
Flowcharts are a diagrammatic method used to represent processes. The
basic symbols used in flowcharts will be introduced as we explore how to
represent the three basic algorithmic constructs of sequence, selection and
iteration.
437531326.doc
© State of New South Wales, Department of Education and Training 2006 5
Reading: Identify algorithm structures
Nassi-Schneiderman diagrams
These diagrams are similar to flowcharts, but have the advantage of
enforcing a particular structure in the program. They can become difficult to
draw when the algorithm includes a lot of nested constructs. We will not be
using NassiSchneidermann diagrams.
437531326.doc
© State of New South Wales, Department of Education and Training 2006 6
Reading: Identify algorithm structures
Accepting inputs
Inputs may come from a variety of sources, such as, keystrokes from a
keyboard, a mouse action, a file stored in the computer, sounds from a
microphone and numerous others. An example is shown below, where the
user is prompted for their surname, and the computer reads it into a variable.
Producing outputs
Outputs may be a file to be stored on disk, information to display on a
screen, information to be printed, sounds to be sent to speakers etc. In the
example below, the text ‘Hello World’ will be sent to the output device (eg a
computer screen).
437531326.doc
© State of New South Wales, Department of Education and Training 2006 7
Reading: Identify algorithm structures
places the number 6 into a variable called x. The variable x will (when the
program executes) be a location in the computer’s memory. Thankfully, you
don’t need to worry about where the number is stored, or how it is
represented — you only need to perform processing with the variable. As
the name suggests, the actual data stored in a variable may change during
the execution of a program or script.
Variables can store numbers or text. The basic data types are numbers and
text.
Numbers can be whole numbers (called integers) such as 2, 27 and
139 or real numbers (numbers that contain an integer and a fraction
shown as one or more decimal places) for example 12.3 and 0.0027.
Text can be single characters such as ‘A’, ‘g’, ‘Y’ or a string of
characters such as ‘Hello World’.
In the example below, the variable called ‘total’ is set to zero; this is a way
of telling the computer to store the value 0 in the memory location or
variable called total.
The name you give to a variable may refer to a storage location that is only
one byte in size or a location that is many bytes in size. You should always
use meaningful names for variables in algorithms to improve the readability
of the algorithm and its resulting program. This is particularly important in
large and complex programs.
437531326.doc
© State of New South Wales, Department of Education and Training 2006 8
Reading: Identify algorithm structures
Performing arithmetic
Computers can perform the arithmetic operations of adding, multiplying,
dividing, subtracting and exponentiation. An example is the following
statement which is a way of telling the computer to multiply the number of
items and the price, placing the result into the variable called total.
Repeating operations
Computers can execute the same set of instructions over and over again.
This is commonly referred to as iteration, repetition or looping. There are
several types of iteration that can be used depending on what you want the
computer to do. A loop can be constructed to execute the same set of
instructions for a certain number of times or to execute the set of
instructions while a condition is true. For example, while the total stays
above 0 the computer is to continuously subtract the new purchase amount
from the total.
437531326.doc
© State of New South Wales, Department of Education and Training 2006 9
Reading: Identify algorithm structures
Sequence
Sequence is where one task is performed sequentially after another.
In pseudocode, each task is on a separate line and has the same indentation
as the other tasks in the sequence. The tasks are executed one after the other
beginning with the task at the top of the sequence and ending with the task
at the bottom of the sequence.
437531326.doc
© State of New South Wales, Department of Education and Training 2006 10
Reading: Identify algorithm structures
437531326.doc
© State of New South Wales, Department of Education and Training 2006 11
Reading: Identify algorithm structures
Pseudocode: Flowchart:
437531326.doc
© State of New South Wales, Department of Education and Training 2006 12
Reading: Identify algorithm structures
Selection
This is where a choice is made between alternative courses of action.
Selection is also known as ‘branching’. In pseudocode, selection is
represented by the IF-THEN-ELSE construct. The simplest form is shown
below.
Pseudocode: Flowchart:
IF condition THEN
<block>
ENDIF
437531326.doc
© State of New South Wales, Department of Education and Training 2006 13
Reading: Identify algorithm structures
Pseudocode: Flowchart:
compares the value of the variable hoursWorked to the constant value 40. If
hoursWorked has a value of, say, 70 then this condition will be TRUE,
because 70 is indeed greater than 40. If hoursWorked contains the value 30
then of course the condition will be FALSE. If hoursWorked contains the
value 40 then the condition is also FALSE.
Here are some more examples of conditions using each of the comparison
operators:
Condition Meaning
x == 3 x is equal to 3
y != 0 y is not equal to 0
cashEarned > cashSpent cashEarned is greater than cashSpent
temperature < 100 temperature is less than 100
count <= 10 count is less than or equal to 10
pressure >= 1500 pressure is greater than or equal to 1500
437531326.doc
© State of New South Wales, Department of Education and Training 2006 14
Reading: Identify algorithm structures
IF condition
THEN
(sequence 1)
ELSE
(sequence 2)
ENDIF
Note 1: You do not need the ELSE part. The ELSE keyword and ‘sequence
2’ are optional.
Note 2: Notice that the sequences for the IF and the ELSE part are indented
the same amount. As mentioned earlier, this ‘style’ is very common and
allows you to see the logic of the algorithm more easily. A very good
programming habit to adopt is to decide upon a standard style and stick to it
when writing algorithms.
437531326.doc
© State of New South Wales, Department of Education and Training 2006 15
Reading: Identify algorithm structures
Pseudocode: Flowchart:
IF condition THEN
(Block 1)
ELSE
(Block 2)
ENDIF
437531326.doc
© State of New South Wales, Department of Education and Training 2006 16
Reading: Identify algorithm structures
437531326.doc
© State of New South Wales, Department of Education and Training 2006 17
Reading: Identify algorithm structures
In the example above, note that ‘fail’ is displayed if the test mark equals 50.
In Selection example 1 and Selection example 2, at least one action is
performed. In example 2, it doesn’t matter what the actual test mark is a
message will be displayed. The test mark simply determines which
alternative message is displayed.
There may be times we want something done when a condition is met but
don’t want anything done if the condition is not met. This action is
performed by the following construct:
IF condition THEN
<block>
ENDIF
Iteration
Iteration is the process of repetition, also known as ‘looping’. Iteration
allows a program to do something again and again, or perform similar
operations on multiple items. Here is a simple loop that ‘counts’ from one to
ten:
SET count = 1
WHILE count <= 10 DO
DISPLAY count
count = count + 1
ENDWHILE
DISPLAY 'bang!'
437531326.doc
© State of New South Wales, Department of Education and Training 2006 18
Reading: Identify algorithm structures
But now, what if you want to compute the pays of all workers in the
company? You can do this by wrapping a loop around the original
algorithm, something like this:
FOR EACH worker DO
SET worker.pay = worker.hoursWorked * hourlyRate
WRITE pay
ENDFOR
Now the same calculation is repeated for each worker. The notation
‘worker.pay’ means the pay for that particular worker.
Types of loops
Most computer languages provide three types of looping statement, which
are the:
1 Pre-test loop — also known as the WHILE loop
2 Post-test loop — eg the REPEAT/UNTIL or DO/WHILE loops
3 Counting loop — also known as the FOR loop.
In fact, we can get by with only the WHILE loop, but these other loops help
to make our code simpler and our intentions clearer.
There are several things you need to consider when deciding upon the best
way to construct loops. What conditions should exist for entry into a loop?
Do you want to execute the loop instructions for a particular number of
times? If you control a loop based upon some condition being true, how will
you modify the condition so that it becomes false (so the loop can stop)?
Answering these questions will assist in selecting the most appropriate loop
for the problem you are solving.
There are a few different types of loops, however, all loops can be described
using the WHILE loop. The WHILE loop executes the instructions inside
the loop if some condition(s) is true and continues executing the instructions
whilst the condition(s) remain true.
The FOR loop is a counting loop. It performs the instructions inside the loop
a certain number of times. The REPEAT/DO and the FOR loops are usually
included in pseudocode and other algorithmic languages because they are
often implemented in programming languages.
437531326.doc
© State of New South Wales, Department of Education and Training 2006 19
Reading: Identify algorithm structures
The WHILE construct is used to specify a loop with a test at the top of the
loop. In pseudocode, the beginning and ending of the loop are commonly
denoted by the two keywords WHILE and ENDWHILE. The general form
is:
WHILE condition DO
<block>
ENDWHILE
This is how it works. The loop is entered only if the condition is true. If the
condition is true, the sequence is performed. After the sequence has been
performed, the condition is checked again. If the condition is still true, the
sequence is performed again. This process continues until the condition is
false.
Pseudocode: Flowchart:
437531326.doc
© State of New South Wales, Department of Education and Training 2006 20
Reading: Identify algorithm structures
Repeat-until loop
This loop is similar to the WHILE loop except that the test is performed at
the bottom of the loop instead of at the top. Two pseudocode keywords,
REPEAT and UNTIL are used. The general form is:
Pseudocode: Flowchart:
REPEAT
<Block>
UNTIL condition
The sequence in this loop is always performed at least once, because the test
is performed after the sequence has already been executed. After the
sequence is performed, the condition is tested and if required, the loop
repeats.
FOR
This loop is a specialised construct in which an index variable is
automatically initialised, incremented and tested. Two pseudocode
keywords, FOR and ENDFOR are commonly used. The general form is:
FOR condition
block
ENDFOR
437531326.doc
© State of New South Wales, Department of Education and Training 2006 21
Reading: Identify algorithm structures
Pseudocode
At the start of the loop, the index variable is set to the starting value. At the
end of each iteration, the index variable is automatically incremented. (Note:
Incrementing refers to increasing the value of a variable, ie adding a number
to a variable. The default increment is usually 1. The loop repeats until the
index value reaches the finish value.
Pseudocode: Flowchart:
437531326.doc
© State of New South Wales, Department of Education and Training 2006 22
Reading: Identify algorithm structures
Did you notice the problem? The number of indentations required to show
the logic can become very deep, and the structure does not really reflect the
problem, because all the alternatives are of a similar nature. A better way to
represent the above construct is to group the ELSE and following IF in a
more linear fashion, and only indent once, as shown below:
437531326.doc
© State of New South Wales, Department of Education and Training 2006 23
Reading: Identify algorithm structures
Structured programming
Constructing algorithms
Any problem that can be solved by a computer can be solved using just
three basic constructs of sequence, selection and iteration.
1 Sequence — This is where one task is performed after another in
predefined order.
2 Selection — This is where a condition is tested and different tasks are
performed depending on whether the test is true or false.
3 Iteration — This is where tasks are perform repeatedly for a certain
number of times or until some condition is met.
It is a remarkable result from computer science that these basic constructs
can be used to implement any programming problem!
437531326.doc
© State of New South Wales, Department of Education and Training 2006 24
Reading: Identify algorithm structures
Nesting IF constructs
The IF-THEN-ELSE construct can be nested inside another IF-THEN-
ELSE construct.
Pseudocode: Flowchart:
Here we first test to see if the cat is whining for food. If it isn’t, we simply
end the testing. If the cat is whining for food then we will execute the other
IF-THEN-ELSE construct:
IF food bowl empty THEN
Feed the cat
ELSE
Point cat at food bowl
ENDIF
437531326.doc
© State of New South Wales, Department of Education and Training 2006 25
Reading: Identify algorithm structures
Pseudocode: Flowchart:
437531326.doc
© State of New South Wales, Department of Education and Training 2006 26
Reading: Identify algorithm structures
Designing algorithms
A simple method for designing a simple program could be:
1 Understand the problem or requirements.
2 Identify the inputs and the outputs and decide what variables are
needed.
3 Determine the calculations/computations that are required to produce
the output.
4 To construct the algorithm, start by assigning input values to the
variables.
5 Then perform the computations.
6 Generate the output.
7 When there are processes to be repeated, add loops.
8 When decisions are to be made, use selection.
Algorithm pre-conditions
Often there are pre-conditions that must be met for an algorithm to function
correctly.
The preconditions to the process of boiling an egg could be:
an egg to boil
the water to boil it in
heat to make the water boil
a saucepan to hold the water.
Without having all these preconditions, the result of executing the boil egg
algorithm will not be the one we want.
437531326.doc
© State of New South Wales, Department of Education and Training 2006 27
Reading: Identify algorithm structures
Summary
437531326.doc
© State of New South Wales, Department of Education and Training 2006 28