Sub topic 1
Why Write Pseudocode?
Pseudocode is a way of writing an algorithm in structured, plain language. It reads a bit like a program, but no compiler or interpreter runs it. Its job is to state what the steps are without the syntax rules, library calls, and type declarations of a real language. This chapter covers what pseudocode is for, who reads it, and when it beats writing real code straight away.
Prerequisites
This chapter assumes you can already:
- Write small programs in at least one language, such as Python, Java, C, or JavaScript.
- Use variables,
ifstatements, andfororwhileloops. - Work with arrays or lists and access elements by index.
- Define and call functions that take parameters and return values.
You don't need any formal computer science background. Later chapters build on these basics to cover pseudocode conventions, trace tables, and desk checking.
What Pseudocode Is, and What It Is Not
A program has to satisfy a machine. Every semicolon, bracket, import, and type has to be correct before you see any result. Pseudocode only has to satisfy a human reader. That changes what you spend your attention on.
Here is a short algorithm that finds the largest value in a list. It appears throughout this tutorial:
FUNCTION FindMax(A, n)
max ← A[1]
FOR i ← 2 TO n DO
IF A[i] > max THEN
max ← A[i]
END IF
END FOR
RETURN max
END FUNCTIONA reader who knows any mainstream language can follow this without being told which language it "really" is. The arrow ← means assignment. The keywords FOR, IF, and RETURN mean what you would expect. The array starts at index 1, which is common in textbooks. Chapter 2 covers these conventions in detail, and chapter 4 covers indexing.
Pseudocode is not:
- Vague prose. "Look through the list and keep the biggest one" describes the idea, but it skips the starting value, the order of comparisons, and what is returned. Pseudocode has to be precise enough that two people would trace it to the same result.
- A standard language. No single official syntax exists. Textbooks, exam boards, and companies each use their own style. What matters is that you stay consistent within one document.
- A replacement for testing. Pseudocode helps you find logic errors before you write code. It does not catch problems that only show up at runtime, such as integer overflow or a slow library call.
Who Reads Pseudocode
The right level of detail depends on the audience. Three readers come up often.
| Reader | What they need | Typical level of detail |
|---|---|---|
| You, before coding | A plan you can check by hand | Detailed enough to trace every variable |
| A teammate or reviewer | The logic, without your language choices | Key steps and data structures, minor details skipped |
| A student or exam marker | A precise, checkable algorithm | Fully specified, in the house style the course uses |
| A future maintainer | Why the code is shaped the way it is | High-level steps, often kept in design docs or comments |
The same algorithm can be written at different levels for different readers. A design document might say sort the orders by date. A plan you are about to trace would spell out the sorting loop, because the loop is where bugs hide.
Why Not Just Write the Code?
Experienced programmers often skip pseudocode and go straight to an editor. For small, familiar tasks that is fine. Pseudocode pays off in other situations.
It separates thinking from typing
When you write real code, you hold two problems in your head: the algorithm and the language. A missing import or a type error pulls you away from the logic. Pseudocode removes the second problem for a while, so all your attention goes to whether the steps are right.
It is cheap to change
Rewriting three lines of pseudocode takes seconds. Rewriting a function that already has tests, callers, and error handling takes longer and is more likely to break something. Early design mistakes cost the least to fix in pseudocode.
It can be checked on paper
You can step through pseudocode with a pencil, writing down each variable's value as you go. This is called tracing, and chapters 7 through 13 cover it in depth. Tracing is effective at finding off-by-one errors, wrong loop conditions, and missed edge cases, the kinds of bugs that compile cleanly and fail quietly.
It is language-neutral
A team that works in Python, Go, and TypeScript can all review the same pseudocode. An algorithm published in a textbook or paper stays readable long after the language its authors used goes out of fashion.
It documents intent
Code shows what the machine does. Pseudocode written before the code records what you meant it to do. When the two disagree, you have found a bug, and you know which one to trust.
When Real Code Is the Better Choice
Pseudocode is a tool, not a ritual. Skip it or keep it short when:
- The task is trivial. Writing pseudocode for "read a file and print each line" adds nothing.
- The hard part is the platform, not the logic. If the difficulty is an API's authentication flow or a framework's lifecycle, pseudocode hides the very details you need to get right.
- You can test faster than you can trace. With a fast REPL and a small function, running ten test cases can beat tracing two by hand. Even then, picking the right test cases uses the same skills (chapter 10).
- Performance depends on real behaviour. Cache effects, memory allocation, and library speed don't show up in pseudocode. You can count abstract operations (chapter 15), but you have to measure real timings.
Where Pseudocode Fits in a Workflow
The diagram below shows one workflow that this tutorial follows. Pseudocode sits between understanding the problem and writing code. Tracing forms a loop that repeats until the logic holds up on every input you chose.
The arrow from "Run tests" back to "Write pseudocode" matters. A failing test can point to a logic error in the design, not only a typo in the translation. Going back to the pseudocode keeps the design and the code in step.
Common Mistakes When Starting Out
- Writing real code and calling it pseudocode. If your "pseudocode" needs
import,self., orlen(), it is tied to one language. Use language-neutral operations, or say what you mean in words such aslength of A. - Being too vague to trace. Lines like
process the dataorhandle errorscan't be traced. If you couldn't fill in a trace table from a line, it needs more detail, or it needs to be named as a separate procedure and refined later (chapter 6). - Mixing conventions. Using
=for assignment on one line and←on the next, or switching between 0-based and 1-based indexing, makes the reader guess. Guessing produces wrong traces. - Leaving out the edges. Pseudocode for FindMax that doesn't say the list must be non-empty hides a real failure case. Write assumptions down, for example
// requires n ≥ 1. - Treating it as throwaway. Once traced, pseudocode is a specification. Keep it near the code, in a design note or a comment block, so future readers can see what the code was meant to do.
Exercises
Exercise
A colleague writes "go through the list and remove duplicates" and calls it pseudocode. Give two specific questions a reader would need answered before they could trace it.
Show answer
Possible questions: Does the result keep the original order of first occurrences? Is the list changed in place, or is a new list returned? How are two values compared for equality (for example, is case ignored for strings)? What happens with an empty list? Any two that pin down behaviour the sentence leaves open are correct.
Exercise
You need to call a payment provider's API, handle its retry rules, and parse its error responses. Would you spend much time on detailed pseudocode first? Explain why or why not.
Show answer
Probably not. The hard part is the platform's specific behaviour (retry rules, error formats, authentication), and pseudocode hides exactly those details. A short high-level outline of the steps can help, but the real work is reading the API documentation and testing against the actual service.
Exercise
The FindMax pseudocode in this chapter starts with max ← A[1]. What assumption about the input does this make, and how would you state it in the pseudocode?
Show answer
It assumes the array has at least one element (n ≥ 1). With an empty array, A[1] does not exist. State it as a precondition, for example a comment line "// requires n ≥ 1" just below the FUNCTION line, or add an explicit check that handles n = 0.