Resources For Teachers For Tutors For Students & Parents Pricing
Year 11 Maths - Specialist (Unit 1 and Unit 2) Logic and algorithms

Algorithms and pseudocode

20 practice questions 0 video lessons Theory + worked examples

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.

Create a free accountTrack your progress and save your work as you go.
Create free account

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
All three constructs in one algorithm: a sequence of lines, a FOR loop (repetition) whose body \(s \leftarrow s+k\) is an accumulator, ending with an OUTPUT.
\(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\)
Tracing the loop keeps a running total. Each pass adds the next \(k\); after \(k=6\) the output is \(s=21\), the sum \(1+2+\cdots+6\).

Pseudocode uses a small, fixed vocabulary. The assignment arrow updates a variable using its old value on the right:

\[ s \leftarrow s + k \qquad\text{means}\qquad s_{\text{new}} = s_{\text{old}} + k \]

A FOR loop that counts from \(1\) to \(n\) runs its body exactly \(n\) times, so a plain counter inside it finishes at:

\[ \text{FOR } i \text{ FROM } 1 \text{ TO } n \;\Rightarrow\; n \text{ iterations} \]

Two loops nested one inside the other multiply their counts, so an inner counter reaches \(a\times b\):

\[ (\text{outer } a)\times(\text{inner } b) = a\,b \text{ passes} \]

Most standard algorithms are one of a few recognisable patterns. Matching the pattern tells you what the pseudocode computes:

PatternBody of the loopComputes
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 bestIF \(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
n!=1×2××n
Assignment is not equality. A line such as \(s \leftarrow s + k\) would be nonsense as an equation, but as an instruction it simply replaces the stored value. Reading \(\leftarrow\) as “becomes” keeps every trace straight.

How to trace pseudocode

  1. Set up a trace table with one column per variable (and a column for the loop counter), plus a row for the starting values.
  2. Execute one line at a time, top to bottom, updating only the variable on the left of each assignment.
  3. At a decision, test the condition and carry out only the branch that is true; skip the others entirely.
  4. 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.
  5. 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\) falsestop → output \(96\)
Example 1 — Sequence: tracing a swap
Trace the pseudocode below, which starts with \(a=7\) and \(b=4\). What are the final values of \(a\) and \(b\)?
a <- 7
b <- 4
a <- a + b
b <- a - b
a <- a - b
OUTPUT a, b
Solution

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\).

Example 2 — Decision: an IF–THEN–ELSE trace
Trace the pseudocode below with input \(x=-6\). What value is output?
INPUT x
IF x > 0 THEN
    s <- 1
ELSE IF x = 0 THEN
    s <- 0
ELSE
    s <- -1
END IF
OUTPUT s
Solution

Test 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\).

Example 3 — Repetition: a factorial and what it computes
Trace the loop below to find the final value of \(f\), and state what the algorithm computes.
f <- 1
FOR i FROM 1 TO 5
    f <- f * i
END FOR
OUTPUT f
Solution

Keep 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\)).

Example 4 — Matching an algorithm: Euclid’s gcd
The algorithm below is run with \(a=48\) and \(b=18\), where \(a \bmod b\) is the remainder when \(a\) is divided by \(b\). Trace it to the output and state what it computes.
WHILE b != 0
    r <- a mod b
    a <- b
    b <- r
END WHILE
OUTPUT a
Solution

Each 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

Reading \(\leftarrow\) as an equation. A line like \(s \leftarrow s + k\) is an instruction, not an equality. It takes the current \(s\), adds \(k\), and stores the result back in \(s\).
Updating variables in the wrong order. In a swap the three lines must run top to bottom; changing the order (or forgetting a variable has already changed) gives the wrong values.
Running more than one branch of a decision. Exactly one branch of an IF–THEN–ELSE is carried out — the first whose condition is true. Once a branch runs, the rest are skipped.
Miscounting loop passes. A FOR loop “FROM 1 TO \(n\)” runs \(n\) times, including \(i=n\). For a WHILE loop, test the condition before each pass and stop the moment it is false.
Confusing mod and div. \(a \bmod b\) is the remainder; \(a \operatorname{div} b\) is the whole-number quotient. Euclid’s algorithm uses the remainder.

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.