Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

PSEUDOCODE STANDARD

Pseudocode is a kind of structured english for describing algorithms. It allows the designer to
focus on the logic of the algorithm without being distracted by details of language syntax. At
the same time, the pseudocode needs to be complete. It describe the entire logic of the
algorithm so that implementation becomes a rote mechanical task of translating line by line
into source code.

In general the vocabulary used in the pseudocode should be the vocabulary of the problem
domain, not of the implementation domain. The pseudocode is a narrative for someone who
knows the requirements (problem domain) and is trying to learn how the solution is
organized. E.g.,

Extract the next word from the line (good)


set word to get next token (poor)

Append the file extension to the name (good)


name = name + extension (poor)

FOR all the characters in the name (good)


FOR character = first to last (ok)

Note that the logic must be decomposed to the level of a single loop or decision. Thus
"Search the list and find the customer with highest balance" is too vague because it takes a
loop AND a nested decision to implement it. It's okay to use "Find" or "Lookup" if there's a
predefined function for it such as String.indexOf().

Each textbook and each individual designer may have their own personal style of
pseudocode. Pseudocode is not a rigorous notation, since it is read by other people, not by the
computer. There is no universal "standard" for the industry, but for instructional purposes it is
helpful if we all follow a similar style. The format below is recommended for expressing your
solutions in our class.

The "structured" part of pseudocode is a notation for representing six specific structured
programming constructs: SEQUENCE, WHILE, IF-THEN-ELSE, REPEAT-UNTIL, FOR,
and CASE. Each of these constructs can be embedded inside any other construct. These
constructs represent the logic, or flow of control in an algorithm.

It has been proven that three basic constructs for flow of control are sufficient to implement
any "proper" algorithm.

SEQUENCE is a linear progression where one task is performed sequentially after another.
WHILE is a loop (repetition) with a simple conditional test at its beginning.
IF-THEN-ELSE is a decision (selection) in which a choice is made between two alternative
courses of action.

Although these constructs are sufficient, it is often useful to include three more constructs:
REPEAT-UNTIL is a loop with a simple conditional test at the bottom.
CASE is a multiway branch (decision) based on the value of an expression. CASE is a
generalization of IF-THEN-ELSE.
FOR is a "counting" loop.
SEQUENCE

Sequential control is indicated by writing one action after another, each action on a line by
itself, and all actions aligned with the same indent. The actions are performed in the sequence
(top to bottom) that they are written.

Example (non-computer)

Brush teeth
Wash face
Comb hair
Smile in mirror
Example
READ height of rectangle
READ width of rectangle
COMPUTE area as height times width
Common Action Keywords
Several keywords are often used to indicate common input, output, and processing
operations.
Input: READ, OBTAIN, GET
Output: PRINT, DISPLAY, SHOW
Compute: COMPUTE, CALCULATE, DETERMINE
Initialize: SET, INIT
Add one: INCREMENT, BUMP
IF-THEN-ELSE

Binary choice on a given Boolean condition is indicated by the use of four keywords: IF,
THEN, ELSE, and ENDIF. The general form is:

IF condition THEN
sequence 1
ELSE
sequence 2
ENDIF
The ELSE keyword and "sequence 2" are optional. If the condition is true, sequence 1 is
performed, otherwise sequence 2 is performed.

Example

IF HoursWorked > NormalMax THEN


Display overtime message
ELSE
Display regular time message
ENDIF
WHILE

The WHILE construct is used to specify a loop with a test at the top. The beginning and
ending of the loop are indicated by two keywords WHILE and ENDWHILE. The general
form is:

WHILE condition
sequence
ENDWHILE
The loop is entered only if the condition is true. The "sequence" is performed for each
iteration. At the conclusion of each iteration, the condition is evaluated and the loop
continues as long as the condition is true.

Example

WHILE Population < Limit


Compute Population as Population + Births - Deaths
ENDWHILE

Example

WHILE employee.type NOT EQUAL manager AND personCount < numEmployees


INCREMENT personCount
CALL employeeList.getPerson with personCount RETURNING employee
ENDWHILE
CASE

A CASE construct indicates a multiway branch based on conditions that are mutually
exclusive. Four keywords, CASE, OF, OTHERS, and ENDCASE, and conditions are used to
indicate the various alternatives. The general form is:

CASE expression OF
condition 1 : sequence 1
condition 2 : sequence 2
...
condition n : sequence n
OTHERS:
default sequence
ENDCASE

The OTHERS clause with its default sequence is optional. Conditions are normally numbers
or characters

indicating the value of "expression", but they can be English statements or some other
notation that specifies the condition under which the given sequence is to be performed. A
certain sequence may be associated with more than one condition.
Example

CASE Title OF
Mr : Print "Mister"
Mrs : Print "Missus"
Miss : Print "Miss"
Ms : Print "Mizz"
Dr : Print "Doctor"
ENDCASE

Example

CASE grade OF
A : points = 4
B : points = 3
C : points = 2
D : points = 1
F : points = 0
ENDCASE

REPEAT-UNTIL

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 keywords, REPEAT and UNTIL are used. The general form
is:

REPEAT
sequence
UNTIL condition
The "sequence" in this type of loop is always performed at least once, because the test is
peformed after the sequence is executed. At the conclusion of each iteration, the condition is
evaluated, and the loop repeats if the condition is false. The loop terminates when the
condition becomes true.

FOR

This loop is a specialized construct for iterating a specific number of times, often called a
"counting" loop. Two keywords, FOR and ENDFOR are used. The general form is:

FOR iteration bounds


sequence
ENDFOR
In cases where the loop constraints can be obviously inferred it is best to describe the loop
using problem domain vocabulary.

Example
FOR each month of the year (good)
FOR month = 1 to 12 (ok)

FOR each employee in the list (good)


FOR empno = 1 to listsize (ok)

NESTED CONSTRUCTS

The constructs can be embedded within each other, and this is made clear by use of indenting.
Nested constructs should be clearly indented from their surrounding constructs.

Example

SET total to zero


REPEAT
READ Temperature
IF Temperature > Freezing THEN
INCREMENT total
END IF
UNTIL Temperature < zero
Print total
In the above example, the IF construct is nested within the REPEAT construct, and therefore
is indented.

INVOKING SUBPROCEDURES

Use the CALL keyword. For example:

CALL AvgAge with StudentAges


CALL Swap with CurrentItem and TargetItem
CALL Account.debit with CheckAmount
CALL getBalance RETURNING aBalance
CALL SquareRoot with orbitHeight RETURNING nominalOrbit

EXCEPTION HANDLING

BEGIN
statements
EXCEPTION
WHEN exception type
statements to handle exception
WHEN another exception type
statements to handle exception
END
Sample Pseudocode

"Adequate"

FOR X = 1 to 10
FOR Y = 1 to 10
IF gameBoard[X][Y] = 0
Do nothing
ELSE
CALL theCall(X, Y) (recursive method)
increment counter
END IF
END FOR
END FOR

"Better"

Set moveCount to 1
FOR each row on the board
FOR each column on the board
IF gameBoard position (row, column) is occupied THEN
CALL findAdjacentTiles with row, column
INCREMENT moveCount
END IF
END FOR
END FOR

(Note: the logic is restructured to omit the "do nothing" clause)

"Not So Good"

FOR all the number at the back of the array


SET Temp equal the addition of each number
IF > 9 THEN
get the remainder of the number divided by 10 to that index
and carry the "1"
Decrement one
Do it again for numbers before the decimal

"Good Enough (not perfect)"

SET Carry to 0
FOR each DigitPosition in Number from least significant to most significant
COMPUTE Total as sum of FirstNum[DigitPosition] and SecondNum[DigitPosition] and
Carry

IF Total > 10 THEN


SET Carry to 1
SUBTRACT 10 from Total
ELSE
SET Carry to 0
END IF

STORE Total in Result[DigitPosition]

END LOOP

IF Carry = 1 THEN
RAISE Overflow exception
END IF

Algorithms

An algorithm is a logical, step-by-step process for solving a problem. Algorithms are


normally written using one of the following conventions:

pseudo-code

flowcharts

written descriptions

program code

An algorithm should be seen as a starting point before writing a program. It should include
the required programming constructs to solve the problem, ie sequence, selection and
iteration. The finished program should follow the steps the algorithm describes.

Before an algorithm can be designed, it is important to check that the problem is completely
decomposed. You can find out more about decomposition on page 10 of this study guide. The
decomposed problem should consider the following questions:

What are the inputs into the problem?

What will be the outputs of the problem?

In what order do instructions need to be carried out?


What decisions need to be made to solve the problem?

Are any areas of the problem repeated?

Only when a problem is properly decomposed and understood can an algorithm design begin.

Pseudo-code

Most programs are developed using programming languages. These languages have
specific syntax that must be used so that the program will run properly.

Pseudo-code is not a programming language. Instead, it is a simple way of describing a set of


programming instructions in a manner that resembles a programming language. Pseudo-code
has its own syntax, some of which is very similar to many actual programming languages.
Any algorithms designed using pseudo-code will not run unless they are converted into an
actual programming language.

This simple pseudo-code algorithm asks a user to input what their favourite subject is:

WHILE answer <> 'computer science' DO SEND 'What is your favourite subject?' TO
DISPLAY RECEIVE answer FROM (STRING) KEYBOARD IF answer = 'computer
science' THEN SEND 'Good choice!' TO DISPLAY ELSE SEND 'Really? '
TO DISPLAY END IF END WHILE
Pseudo-code is a simple way of describing a set of programming instructions in a
manner that resembles a programming language.
Advantages and disadvantages of pseudo-code

Designing an algorithm in pseudo-code has advantages because:

• it can be quickly and easily converted into an actual programming language as it is


similar to a programming language
• it is fairly easy to understand, even for non-programmers
• it does not matter if there are errors in the syntax - it is usually still obvious what is
intended
• changes to the design can be incorporated quite easily
Pseudo-code also has its disadvantages:

• It can be hard to see how a program flows. For example, where does following one
path as opposed to another take the program?
• It can be time consuming to produce.
Pseudo-code covers many areas of programming and there are different variants. The specific
pseudo-code syntax for the Edexcel GCSE Computer Science course can be found at the end
of the Edexcel specification at Appendix 1.
Flowcharts

A flowchart is a diagram that shows an overview of a program. Flowcharts normally use


standard symbols to represent the different types of instructions. These symbols are used to
construct the flowchart and show the step-by-step solution to the problem. Flowcharts are
sometimes known as flow diagrams.

Common flowchart symbols

Table with common flow diagram symbols, their names and their usage

Flowcharts can be used to plan out programs. This simple flowchart maps out an algorithm
for a program that prints the numbers 1 to 10:
A flow diagram mapping out an algorithm for a program that prints the numbers 1-10

Advantages and disadvantages of using flowcharts

Designing an algorithm using a flowchart has advantages because:

it is easy to see how a program flows

flowcharts follow an international standard - it is easy for any flowchart user to pick up a
diagram and understand it

Flowcharts also have their disadvantages:

with a large program, the diagrams can become huge and therefore difficult to follow

any changes to the design may mean a lot of the diagram has to be redrawn

Written descriptions

Algorithms can also be ‘written descriptions’. There is no defined format for this, so
programmers are free to write as they wish. Short sentences are best. An algorithm should be
easy to understand and have no unnecessary detail. A recipe is a good example of clear
instructions, eg ‘stir’ and ‘bake’. A written description should include all the elements found
when decomposing the problem.

Advantages and disadvantages of using written descriptions

Designing an algorithm using written descriptions has advantages because:

there is no need to worry about using correct syntax

it feels more natural to use normal writing

The main disadvantage of a written description is that there is a temptation to use too many
words. This makes the algorithm descriptive rather than instructional.

Program code

Program code can also be used to create an algorithm. In this context it is called draft
program code. There is no requirement for an algorithm to compile and run so errors are
acceptable so long as the meaning is clear.
Designing an algorithm using program code has the following advantages:

• program code is very familiar


• the syntax doesn’t have to be completely correct
• all the possible elements required will be present in the language - unlike pseudo-code
Program code algorithms also have their disadvantages:

• it is possible for the whole design stage of creating an algorithm to be missed


• decomposition might not have been completed

Standard algorithms

Since computers were created, users have devised programs, many of which, in part, needed
to do the same thing. As a result, standard algorithms have evolved and been adopted in many
programs.

Two types of algorithm that are often used are searches and sorts. Searches enable a data set
to be examined and a specific item to be found. Sorts enable a data set to be sorted into order.

Standard searching algorithms include:

• linear search
• binary search
Standard sorting algorithms include:

• bubble sort
• merge sort

Linear search

A linear search is the simplest method of searching a data set.

Starting at the beginning of the data set, each item of data is examined until a match is made.
Once the item is found, the search ends. If there is no match, the algorithm must deal with
this.

A written description algorithm for a linear search might be:

1. Find out the length of the data set.


2. Set counter to 0.
3. Examine value held in the list at the counter position.
4. Check to see if the value at that position matches the value searched for.
5. If it matches, the value is found. Send a message and end the search.
6. If not, increment the counter by 1 and go back to step 3 until there are no more
items to search.
7. If all the items have been checked and no match is found, send a message.
Consider this list of unordered numbers:

Suppose we were to search for the value 2 in this list. The search would start at position 0 and
check the value held there, in this case 3. 3 does not match 2, so we move on to the next
position.

The value at position 1 is 5. 5 does not match 2, so we move on to the next position.

The value at position 2 is 2 - a match. The search ends.

A linear search in pseudo-code might look like this:

SET find TO 2
SET found TO False
SET length TO LENGTH(list)
SET counter TO 0
WHILE found = False AND counter < length DO
IF list[counter] = find THEN
SET found TO True
SEND 'Found at position' & counter
TO DISPLAY
ELSE
SET counter TO counter + 1
END IF
END WHILE
IF found = False THEN
SEND 'Item not found' TO DISPLAY
END IFF
Although simple, a linear search can be quite inefficient. Suppose the data set contained 100
items of data, and the item searched for happens to be the last item in the set. All of the
previous 99 items would have to be searched through first.

However, linear searches have the advantage that they will work on any data set, whether it is
ordered or unordered.
Binary search
A binary search is an efficient method of searching an ordered list. It will not work on a list
that has not been sorted first.

A written description of a binary search algorithm is:

Start by setting the counter to the middle position in the list.


If the value held there is a match, the search ends and a message is sent.
If the value at the midpoint is less than the value to be found, the list is divided in half, the
lower half of the list is ignored and the search keeps to the upper half of the list.
Otherwise, if the value at the midpoint is greater than the value to be found, the upper half of
the list is ignored and the search keeps to the lower half of the list.
The search moves to the midpoint of the remaining items. Steps two to four continue until a
match is made or there are no more items to be found and a message is sent.
Consider this list of ordered numbers:

Table with list of ordered numbers


Consider searching for the value 11.

The midpoint is found by adding the lowest position to the highest position and dividing by 2.

Highest position (8) + lowest position (0) = 8

8/2 = 4

NOTE - if the answer is a decimal, round up. For example, 3.5 becomes 4. An alternative is
to round down, but be consistent.

Check at position 4, which has the value 7.

7 is less than 11, so the bottom half of the list - including the midpoint - is discarded.
Table with list of ordered numbers, the first five numbers have been discarded
The new lowest position is 5.

Highest position (8) + lowest position (5) = 13

13/2 = 6.5, which rounds up to 7

Check at position 7, which has the value 14.

14 is greater than 11, so the top half of the list (including the midpoint) is discarded.

Table with list of ordered numbers, the first five numbers and the last two numbers have been
discarded
The new highest position is 6.

Highest position (6) + lowest position (5) = 11

11/2 = 5.5, which rounds up to 6

Check at position 6.

The value held at position 6 is 11 - a match. The search ends.

A binary search in pseudo-code might look like this:

SET find TO 11
SET found TO False
SET length TO LENGTH(list)
SET lowerBound TO 0
SET upperBound TO length
WHILE found = False DO
midpoint = (upperBound + lowerBound) DIV 2
IF list[midPoint] = find THEN
SEND 'Found at' & midpoint TO DISPLAY
SET found TO True
ELSE
IF list[midPoint]> item THEN
SET upperBound TO midpoint - 1
ELSE
SET lowerBound TO midpoint + 1
END IF
END IF
END WHILE
IF found = False THEN
SEND 'Not found' TO DISPLAY
END IF

A binary search is a much more efficient algorithm than a linear search. In an ordered list of
every number from 0 to 100, a linear search would take 99 steps to find the value 99. A
binary search would only require 7 steps.

However, a binary search can only work if a list has been sorted into order.
Bubble sort
A bubble sort is the simplest of the sorting algorithms. However, it is an inefficient sort for
anything but a small list because of the number of comparisons required.

A written description algorithm of a bubble sort is:

Start at the beginning of the list.


Compare the first value in the list with the next one up. If the first value is bigger, swap the
positions of the two values.
Move to the second value in the list. Again, compare this value with the next and swap if the
value is bigger.
Keep going until the there are no more items to compare. Note - the last item checked in the
list is now sorted, so ignore this next time.
Go back to the start of the list.
Each run through the list, from start to finish, is known as a pass. The bubble sort continues
until a pass is made where no values have been swapped. At this point, the list is sorted.

Consider this unsorted list:

Table with a list of unsorted numbers


The value at position 0 is 9, and the value at position 1 is 4. 9 is bigger than 4, so the two
items are swapped. The list is now:

Table with a list of unsorted numbers, the numbers at positions zero and one have been
swapped
Then move up to the next position, position 1. The value at position 1 is 9, and the value at
position 2 is 2. 9 is bigger than 2, so the two items are swapped. The list is now:

Table with a list of unsorted numbers, the numbers at positions one and two have been
swapped
Move up to the next position, position 2. The value at position 2 is 9, and the value at position
3 is 6. 9 is bigger than 6, so the two items are swapped. The list is:

Table with a list of unsorted numbers, the numbers at positions two and three have been
swapped
Now move up to the next position, position 3. The value at position 3 is 9, and the value at
position 4 is 5. 9 is bigger than 5, so the two items are swapped. The list is now:

Table with a list of unsorted numbers, the numbers at positions three and four have been
swapped
The first pass is now complete. However, this list may still be unsorted, so another pass takes
place.

After the second pass, the list is now:


Table with a list of sorted numbers
The second pass is now complete. However, this list may still be unsorted, so another pass
takes place.

After the third pass, the list is the same, as all items were in order after the second pass.
However, a bubble sort continues until no swaps are made in a pass. During the third pass, no
swaps occurred, so now the sort knows that all items are in order.

A pseudo-code algorithm for a bubble sort might be:

SET counter TO 0
SET swapped TO True
SET swaps TO 0
SET length TO LENGTH(list)
WHILE swapped = True DO
WHILE counter < length – 1 DO
IF list[counter] > list[counter + 1]
THEN
SET temp TO list[counter]
SET list[counter] TO
list[counter + 1]
SET list[counter + 1] TO temp
SET swaps TO swaps + 1
END IF
SET counter TO counter + 1
SET length TO length - 1
END WHILE
IF swaps = 0 THEN
SET swapped TO False
ELSE
SET swaps TO 0
SET counter TO 0
END IF
END WHILE

You might also like