Algorithms and pseudocode
Master algorithms and pseudocode in Year 11 VCE Specialist Mathematics. An algorithm is a finite list of unambiguous steps, and pseudocode writes one down in structured, language-neutral English built from sequence, decision and repetition. It sits in the Algebra, number and structure area of study of the VCE Mathematics Study Design (VCAA), within the Logic and algorithms topic of Unit 1.
You will learn to read IF–THEN–ELSE decisions and FOR and WHILE loops, trace pseudocode with a trace table to find its output, and recognise what an algorithm computes — a sum, factorial, maximum or gcd.
Theory
An algorithm is a finite list of unambiguous steps that solves a problem, and pseudocode is a plain, language-neutral way of writing one down. In Year 11 Specialist Mathematics you build algorithms from three constructs — sequence, decision and repetition — and you trace pseudocode line by line to find its output, its final variable values, and what it computes.
An algorithm is a finite, ordered set of clear steps that always produces a result for its inputs. Pseudocode describes an algorithm in structured English rather than any real programming language, so it reads the same to everyone. A variable is a named store for a value, and the assignment \(x \leftarrow 5\) means “put the value \(5\) into \(x\)” — the arrow is not an equation.
Every algorithm is assembled from three fundamental constructs:
- Sequence — steps carried out once each, one after another, in order.
- Decision (selection) — a choice between alternatives using a condition, written as an IF … THEN … ELSE block. Only the branch whose condition is true is carried out.
- Repetition (iteration) — a block repeated by a loop. A FOR loop repeats a fixed number of times; a WHILE loop repeats as long as a condition stays true.
To trace pseudocode you play computer: work through the lines in order, writing the current value of every variable in a trace table, and follow decisions and loops exactly as written until you reach the OUTPUT or the end. The final numbers in the table are the answer.
Reading a trace also tells you what the algorithm computes. Common patterns build a running total (a sum), a running product (such as a factorial), the largest or smallest value in a list, or the greatest common divisor of two numbers.
s <- 0
FOR k FROM 1 TO 6
s <- s + k
END FOR
OUTPUT s| \(k\) | \(s \leftarrow s + k\) |
|---|---|
| start | \(0\) |
| \(1\) | \(0+1=1\) |
| \(2\) | \(1+2=3\) |
| \(3\) | \(3+3=6\) |
| \(4\) | \(6+4=10\) |
| \(5\) | \(10+5=15\) |
| \(6\) | \(15+6=21\) |
Pseudocode uses a small, fixed vocabulary. The assignment arrow updates a variable using its old value on the right:
A FOR loop that counts from \(1\) to \(n\) runs its body exactly \(n\) times, so a plain counter inside it finishes at:
Two loops nested one inside the other multiply their counts, so an inner counter reaches \(a\times b\):
Most standard algorithms are one of a few recognisable patterns. Matching the pattern tells you what the pseudocode computes:
| Pattern | Body of the loop | Computes |
|---|---|---|
| Accumulator | \(s \leftarrow s + k\) from \(s=0\) | the sum \(1+2+\cdots+n\) |
| Running product | \(f \leftarrow f \times i\) from \(f=1\) | the factorial \(n! = 1\times2\times\cdots\times n\) |
| Keep the best | IF \(L[i] > m\) THEN \(m \leftarrow L[i]\) | the maximum of a list |
| Euclid | \(r \leftarrow a \bmod b\), then \(a \leftarrow b,\ b \leftarrow r\) | the \(\gcd\) of two numbers |
How to trace pseudocode
- Set up a trace table with one column per variable (and a column for the loop counter), plus a row for the starting values.
- Execute one line at a time, top to bottom, updating only the variable on the left of each assignment.
- At a decision, test the condition and carry out only the branch that is true; skip the others entirely.
- At a loop, repeat the body, advancing the counter or re-testing the WHILE condition each pass, and stop as soon as the condition fails.
- Read off the result at OUTPUT (or the end) — the last values in the table — and, if asked, name the quantity the pattern computes.
For example, tracing the doubling loop below keeps one column for \(x\); it stops the first time the WHILE test is false:
x <- 3
WHILE x < 50
x <- 2 * x
END WHILE
OUTPUT x| Test \(x < 50\) | \(x \leftarrow 2x\) |
|---|---|
| \(3<50\) true | \(6\) |
| \(6<50\) true | \(12\) |
| \(12<50\) true | \(24\) |
| \(24<50\) true | \(48\) |
| \(48<50\) true | \(96\) |
| \(96<50\) false | stop → output \(96\) |
a <- 7 b <- 4 a <- a + b b <- a - b a <- a - b OUTPUT a, b
Carry out each assignment in order, updating one variable at a time:
| \(a\) | \(=\) | \(7,\quad b = 4\) |
| \(a \leftarrow a+b\) | \(=\) | \(7+4 = 11\) |
| \(b \leftarrow a-b\) | \(=\) | \(11-4 = 7\) |
| \(a \leftarrow a-b\) | \(=\) | \(11-7 = 4\) |
The three assignments have exchanged the two values without a temporary variable.
The final values are \(a = 4\) and \(b = 7\) — the algorithm swaps \(a\) and \(b\).
INPUT x
IF x > 0 THEN
s <- 1
ELSE IF x = 0 THEN
s <- 0
ELSE
s <- -1
END IF
OUTPUT sTest each condition in order and take the first branch that is true:
| \(x\) | \(=\) | \(-6\) |
| \(x > 0\ ?\) | \(=\) | \(-6 > 0 \text{ is false}\) |
| \(x = 0\ ?\) | \(=\) | \(-6 = 0 \text{ is false}\) |
| \(\text{else branch}\) | \(\Rightarrow\) | \(s = -1\) |
Only one branch runs; the others are skipped completely.
The output is \(-1\). This algorithm returns the sign of \(x\).
f <- 1
FOR i FROM 1 TO 5
f <- f * i
END FOR
OUTPUT fKeep a running product, multiplying by each \(i\) in turn:
| \(i\) | \(f \leftarrow f \times i\) |
|---|---|
| start | \(1\) |
| \(1\) | \(1\times1=1\) |
| \(2\) | \(1\times2=2\) |
| \(3\) | \(2\times3=6\) |
| \(4\) | \(6\times4=24\) |
| \(5\) | \(24\times5=120\) |
What it computes:
| \(f\) | \(=\) | \(1\times2\times3\times4\times5\) |
| \(=\) | \(5!\) |
The final value is \(f = 120\); the algorithm computes \(5! = 120\) (the product \(1\times2\times\cdots\times n\)).
WHILE b != 0
r <- a mod b
a <- b
b <- r
END WHILE
OUTPUT aEach pass replaces \((a,b)\) with \((b,\ a \bmod b)\) until \(b=0\):
| \(a\) | \(b\) | \(r = a \bmod b\) |
|---|---|---|
| \(48\) | \(18\) | \(48 \bmod 18 = 12\) |
| \(18\) | \(12\) | \(18 \bmod 12 = 6\) |
| \(12\) | \(6\) | \(12 \bmod 6 = 0\) |
| \(6\) | \(0\) | stop — output \(a\) |
The output is \(a = 6\); the algorithm computes \(\gcd(48,18) = 6\), the greatest common divisor of the two inputs.
Common pitfalls
Frequently asked questions
What is an algorithm?
An algorithm is a finite, ordered list of clear, unambiguous steps that solves a problem or computes a result for its inputs. It must always finish and give the same answer for the same inputs.
What is pseudocode?
Pseudocode is a way of writing an algorithm in structured, plain English rather than a real programming language. It uses a small vocabulary — assignment \(\leftarrow\), IF/ELSE, FOR, WHILE, OUTPUT — so anyone can read it.
What are the three fundamental constructs?
Sequence (steps done once, in order), decision or selection (an IF–THEN–ELSE choice controlled by a condition), and repetition or iteration (a block repeated by a FOR or WHILE loop). Every algorithm is built from these three.
How do I trace pseudocode?
Make a trace table with a column for each variable, then execute the lines top to bottom, writing each new value as it changes. Follow decisions and loops exactly as written, and read the answer at OUTPUT.
What does the arrow \(\leftarrow\) mean?
It is assignment: \(x \leftarrow 5\) stores the value \(5\) in the variable \(x\). In \(s \leftarrow s + k\), the old value of \(s\) is used on the right and the new total is stored back in \(s\).
How can I tell what an algorithm computes?
Trace it and look at the pattern. A running total gives a sum; a running product gives a factorial; keeping the larger value each pass finds a maximum; repeatedly taking \(a \bmod b\) gives a greatest common divisor.