University of Auckland · FACULTY OF COMPUTER SCIENCE

COMPSCI110 · Introduction to Computer Systems

- one subject, every graph, every model, every mark
Computer Science14 Chapters5-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 5 of 11 · COMPSCI 110

Algorithms & Pseudocode

Module 5 of University of Auckland COMPSCI110 steps back from hardware to the idea of a computation itself: what an algorithm formally is, and how to express one in pseudocode before writing real code, using just three building blocks — sequence, selection and iteration. Important assessment note: Module 5 is examined only in the mid-semester test — it is NOT on the final exam. So learn it thoroughly for the test, then know you can de-prioritise it during final-exam revision.

In this chapter

What this chapter covers

  • 01The formal definition of an algorithm: a well-ordered collection of unambiguous, effectively computable operations that produces a result and halts in finite time
  • 02Pseudocode's role: between natural language (ambiguous) and a programming language (fussy); it designs algorithms and is not executed
  • 03The three primitive constructs — Sequential (one task at a time), Conditional/Selection (if-then-else on a True/False test), Iterative/Loop (while: a continuation condition + a body)
  • 04while vs do/while: while may run the body zero times; do/while runs the body at least once
  • 05Assignment uses ← (an arrow); = is reserved for comparison — a common trap in trace questions
  • 06Desk-checking pseudocode with a trace table: track each variable's value line by line to predict the output
  • 07ASSESSMENT NOTE: Module 5 appears only in the mid-semester test (Teleform MCQ, restricted book), not on the final exam
Worked example · free

Trace a factorial loop by hand

Q [4 marks]. Desk-check this pseudocode for input n = 4 and give the output.
input n · fact ← 1 · i ← 1 · while i ≤ n { fact ← fact × i · i ← i + 1 } · output fact
  • +1Initialise before the loop: read n = 4, set fact ← 1 and i ← 1. Note ← is assignment, not comparison.
  • +1Iterations 1–2: i=1, condition 1 ≤ 4 true → fact = 1 × 1 = 1, i ← 2. i=2, 2 ≤ 4 true → fact = 1 × 2 = 2, i ← 3.
  • +1Iterations 3–4: i=3, 3 ≤ 4 true → fact = 2 × 3 = 6, i ← 4. i=4, 4 ≤ 4 true → fact = 6 × 4 = 24, i ← 5.
  • +1Loop guard now: i = 5, condition 5 ≤ 4 is false → exit the loop and output fact = 24. That is 4! = 24. ✓
Output = 24 (= 4!). The loop multiplies fact by i for i = 1, 2, 3, 4, then the guard 5 ≤ 4 fails and the loop ends.
Sia tip — In a trace table, update one variable per line and always re-test the loop condition after the body — the most common slip is running the body one time too many or too few. Watch the boundary: with i ≤ n the body runs for i = 1…n; changing it to i < n would stop at 3 and give 6, the wrong answer. Ask Sia to give you a fresh loop and mark your trace table.
Glossary

Key terms

Algorithm
A well-ordered collection of unambiguous and effectively computable operations that, when executed, produces a result and halts in a finite amount of time. All four properties matter: well-ordered (definite step order), unambiguous/computable, produces an observable result, and terminates (no infinite loop).
Pseudocode
A structured, language-neutral notation for describing algorithms that sits between natural language and a real programming language. It is precise enough to be unambiguous but is meant to be read and reasoned about, not compiled or run.
Sequence
The simplest control structure: operations carried out one after another, in order — a computation, an input or an output at each step. Everything else is built by nesting selection and iteration around sequences.
Selection (conditional)
The if-then-else structure: evaluate a True/False condition and take one branch or the other. It lets an algorithm make decisions, and it is one of the three primitive constructs from which any algorithm can be built.
Iteration (loop)
Repeating a body of operations. A while loop tests its continuation condition before each pass, so the body can run zero times; a do/while tests after, so the body always runs at least once. Getting the boundary condition right is the crux of trace questions.
Assignment (←)
Storing a value in a variable, written with a left arrow: x ← 5 puts 5 into x. It is deliberately distinct from = , which pseudocode reserves for comparison (testing equality). Confusing the two is a frequent source of trace-table errors.
FAQ

Algorithms & Pseudocode FAQ

Is Module 5 on the final exam?

No. Algorithms & pseudocode (Module 5) is assessed only in the mid-semester test, not on the final exam — the final's per-module breakdown covers Modules 1–4 and 6–11 and omits Module 5 entirely. So study it fully for the test, but during final-exam revision you can safely spend that time on the heavier exam modules such as Operating Systems and Compilers. Always confirm the current split on Canvas.

What are the three control structures, and why only three?

Sequence (do steps in order), selection (if-then-else — choose a branch on a condition) and iteration (loop — repeat a body while a condition holds). These three are provably enough to express any algorithm, which is why pseudocode needs no more than them. Programs in real languages are just these constructs nested and combined, with concrete syntax added.

Can AI help me with algorithms and pseudocode in COMPSCI110?

Yes, as a study aid. Sia can build a trace table with you row by row, explain why a loop runs n times rather than n−1, and turn an English description into clean pseudocode using the three constructs. Use it to learn the method and rehearse for the mid-semester test — it explains and checks your reasoning but does not sit the test for you, and the University of Auckland's academic-integrity rules apply to that controlled assessment.

What's the difference between a while loop and a do/while loop?

A while loop checks its condition first, so if the condition is false at the start the body never runs (zero iterations are possible). A do/while runs the body once and then checks, so the body always executes at least once. In a trace question, spot which one you have before you start: it changes whether the first pass is guaranteed and can change the final answer.

What makes a set of steps an actual algorithm?

It must satisfy the formal definition: the steps are well-ordered (their sequence is definite), each is unambiguous and effectively computable, the process produces an observable result, and it halts in a finite time. A recipe that could loop forever, or that contains an ambiguous 'do it nicely' step, fails the definition — termination and unambiguity are the properties students most often overlook.

Study strategy

Exam move

Because Module 5 is test-only, front-load it: master it before the mid-semester test and don't carry it into final-exam revision. The examinable skill is desk-checking, so practise trace tables until they're mechanical — one variable per column, update one line at a time, and always re-test the loop condition after each pass. Drill the boundary cases that trip people up: i ≤ n versus i < n, and while (test-first, possibly zero passes) versus do/while (test-after, at least one pass). Keep the notation straight — ← assigns, = compares — because MCQ distractors are built on that confusion. Memorise the four properties of the formal algorithm definition (well-ordered, unambiguous/computable, produces a result, halts) as they're a favourite short-answer item, and rehearse turning a plain-English task into pseudocode using only sequence, selection and iteration.

Working through Algorithms & Pseudocode in COMPSCI 110? Sia is AskSia’s AI Computer Science tutor — ask any COMPSCI 110 Algorithms & Pseudocode question and get a clear, step-by-step explanation grounded in how COMPSCI 110 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.

A+Everything unlocked
Unlocks this Bible + all 12 of your University of Auckland subjects - and 1,000+ Bibles across every Australian university.
Sia - your COMPSCI110 tutor, unlimited, worked the way the exam marks it
The full 5-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$25/ month
30-day money-back · cancel in one tap · how it works
Unlock the full COMPSCI110 Bible + 12 University of Auckland subjects解锁完整 COMPSCI110 Bible + University of Auckland 12 门科目
$25/mo