You are on page 1of 3

Pseudocode

Please note that pseudocode can vary and the later examples do not strictly need to be
followed if you know what is right and wrong.

All pseudocode must follow the following structure:

List your input


parameters between
brackets after the
Algorithm 1. algorithm_name(parameterOne, method name. This
keyword parameterTwo, parameterThree) can be empty if
Algorithm and there are no input
the number Input: parameterOne is of type X, parameters.
of the algorithm parameterTwo is of type Y, parameterThree
you are currently
presenting. is of type Z Always list out each
input variable this way
1. Lines of pseudocode and declare what type
it is. If you have no
2. Go here input variables this can
Always number 3. For example you might create and populate be left empty i.e
the lines of your written as:
an array named S here Input:
actual computations.

Output: S

This can be empty if nothing


needs to be output. Do not ever
output a variable that either you
have not defined in the
pseudocode or is not an input
variable. This should be a
SINGLE variable. If it is left empty
write:
Output:
With nothing following.

General rules

• Do NOT EVER use a variable name in your pseudocode that you have not
previously defined or is not an input parameter.

• NEVER use a method in your pseudocode if you have not defined this method as
another algorithm. i.e. never use .length() or .append() unless you create an
algorithm and define them or explain what they do.

• ALWAYS indent properly after a loop or if statement.

• NEVER put spaces in your method names or variable names.

• Method names and variables names SHOULD NOT start with a capital letter.

• Your output MUST be a single variable name or empty if there is no output.


• Your variable names SHOULD be named something meaningful.

• DO NOT change the capitalisation or spelling of a variable name as you use it


throughout your pseudocode.

• ALWAYS end your loops and if statements with an END FOR, END WHILE or
END IF.

Examples

Algorithm 1. backwards(inputArray)
Input: inputArray is of type integer array

1. end = |inputArray|-1
2. For i = end to i = 0
3. print inputArray[i]
4. i = i – 1
5. End For

Output:

__________________________________________________

Algorithm 2. backwards_with_output(inputArray)
Input: array is of type integer array

1. Let array_two[0...|inputArray|-1] be a new array


2. count = 0
3. For i = |inputArray|-1 to i = 0
4. array_two[count] = inputArray[i]
5. i = i – 1
6. count = count + 1
7. End For

Output: array_two

___________________________________________________

Algorithm 3. comparison(numberOne, numberTwo, numberThree)


Input: numberOne is of type integer, numberTwo is of type
integer, numberThree is of type integer

1. If numberOne = numberTwo = numberThree Then


2. print “All equal.”
3. Else If numberOne = numberTwo Then
4. print “One equal to two.”
5. Else If numberOne = numberThree Then
6. print “one equal to three.”
7. Else If numberTwo = numberThree Then
8. print “Two equal to three.”
9. Else print “None equal.”
10 End If

Output:
___________________________________________________

Algorithm 4. untilTen(end)
Input: end is of type integer

1. i = 4
2. While i <= end
3. Print “Has not reached the end yet”
4. i = i + 1
5. End While

Output:
___________________________________________________

Algorithm 5. addToSet(originalList)
Input: originalList is of type list

1. newSet = ∅
2. For i = 0 to i = |originalList|
3. newSet = newSet ∪ originalList[i]
4. End For

Output: newSet

___________________________________________________

Algorithm 5. usingAnotherMethod(one, two, three, four)


Input: one is of type integer, two is of type integer, three is
of type integer, four is of type integer

1. add = one + two + three + four


2. multiply = one * two * three * four
3. Let newList [0...1] be a new array
4. newList[0] = add
5. newList[1] = multiply
6. setCallingMethod = addToSet(newList)

Output: setCallingMethod
Note how here we are calling another
method that we have defined earlier on.

You might also like