Source: https://www.vcaa.vic.edu.au/curriculum/vce/vce-study-designs/Pages/PseudoCode.aspx (2023)
Pseudocode is a tool for representing algorithms without the use of a particular programming language and related syntax.
It is written in a combination of plain English and common symbols, and describes, in a detailed step-by-step manner, the processes used in the algorithm.
There are some basic principles to be followed in using pseudocode which include:
Have only one statement per line.
Use indentation to show the hierarchy of processes within an algorithm such as repeating sections and conditional decisions.
End nested processes with an end keyword (end if, end while).
Values can be entered into an algorithm as part of the algorithm name or as input within the algorithm.
A variable is a string of one or more letters that acts as a placeholder that can be assigned different values. For example:
x ← 5 means ‘assign the value 5 to the variable x’.
x ← x + 1 means ‘assign the value x + 1 to the variable x’.
if – then blocks provide a means of making decisions within an algorithm. Certain instructions are only followed if a condition is true.
if condition is true then
follow these instructions
end if
We can expand this process by specifying alternative instructions if the condition is false.
if condition is true then
follow these instructions
else
follow these instructions
end if
Algorithm: minimum of two numbers
input a, b
if a ≤ b then
print a
else
print b
end if
Further decisions can be added inside if – then blocks to include more conditions are nested (indented) within the previous block.
if first condition is true then
follow these instructions
else if second condition is true then
follow these instructions
else
follow these instructions
end if
Loops are used to repeat a process. How many repetitions (iterations) take place is controlled by either specifying and counting the number of times (a for loop) or by specifying a condition which must be met for the process to continue, otherwise it ends (a while loop). An example of each of these approaches is shown below:
For loops provide a means of repeatedly executing the same set of instructions in a controlled way. A counter (i) is increased by one, each time through the loop.
for i from 1 to n
follow these instructions
end for
Note: We will follow the convention that `from 1 to n’ is inclusive. For example: for i from 1 to 3, i takes values 1, 2 and 3,
Algorithm: First 5 perfect squares
for i from 1 to 5
print i2
end for
While loops provide another means of repeatedly executing the same set of instructions in a controlled way. This is achieved by performing iterations as long as some condition remains true.
while condition is true
follow these instructions
end while
Algorithm: Perfect squares less than 1000
x ← 1
while x2 < 1000
x ← x + 1
print (x – 1)2
end while
Are sections of pseudocode that can be used to complete a specific task. Once a function is defined, it can be used (called) within another algorithm. A function takes one or more input values and returns an output value.
define function_name(input for function):
follow these instructions
return output
Example:
define factorial(n):
product ← 1
for i from 1 to n
product ← product × i
end for
return product