Video lecture
Watch on YouTube(opens in a new tab)Sub topic 7
Describing Algorithms with Pseudocode
This chapter assumes you can read simple Python, know what the remainder (modulo) operation does, and have seen sequence, selection and iteration in the previous chapter. Here you will turn the plain-language version of Euclid's algorithm into pseudocode. Pseudocode is a compact, code-like notation for algorithms that no compiler runs.
Why pseudocode exists
Plain language is easy to read, but it gets wordy, and it can leave unclear which steps a "go back to step 1" repeats. Real code is exact, but it brings in language details like type declarations, imports and syntax quirks that have nothing to do with the algorithm. Pseudocode sits between the two. It borrows the control structures of programming languages and leaves out everything else.
Euclid's algorithm in pseudocode
Here is the same GCD algorithm from earlier chapters:
function GCD(a, b)
// Precondition: a >= 0, b >= 0, not both zero
// Postcondition: returns gcd(a, b)
while b != 0 do
r <- a mod b
a <- b
b <- r
end while
return a
end functionCompare it with the numbered steps. "If b is 0, stop" and "go back to step 1" have become a single while loop. A reader can now see the repeated part at a glance. The specification now sits in comments at the top, so the contract stays next to the steps.
Common conventions
Pseudocode has no official standard, but most textbooks agree on the following:
| Construct | Typical form | Meaning |
|---|---|---|
| Assignment | `x <- e` | Evaluate `e`, store it in `x` |
| Comparison | `=`, `!=`, `<`, `>=` | Tests that give true or false |
| Selection | `if ... then ... else ... end if` | Choose one branch |
| Iteration | `while ... do ... end while`, `for i <- 1 to n do` | Repeat a block |
| Procedure | `function Name(params) ... return v` | A named, reusable algorithm |
| Comment | `// text` | Notes and specification, not steps |
The arrow <- is used for assignment so that = can mean only comparison. In many programming languages = does both jobs, and beginners mix them up as a result.
From pseudocode to code
A good test of pseudocode is whether a programmer can translate it line by line. The Python version follows directly:
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
print(gcd(48, 18)) # 6Python's tuple assignment a, b = b, a % b replaces the three pseudocode lines that use the temporary r. That is a language feature. Pseudocode keeps the temporary variable so the update order is explicit for readers who use any language.
Common errors
- Writing real code and calling it pseudocode. If the text needs
importstatements or semicolons, you have gone too far. - Steps that are too vague. "Process the list" fails the definiteness property. Each line should be an operation a reader knows how to carry out.
- Unclear block boundaries. Use indentation plus
end whileorend ifso readers can see where a loop body ends. - Hidden simultaneous updates. Writing
a <- bthenb <- a mod bis a bug, becauseahas already changed. Use a temporary variable, asrdoes above.
Exercise
Write pseudocode for a function Sum(xs) that returns the sum of a list of numbers xs (which may be empty). Include a precondition and a postcondition as comments, and use a for loop.
Show answer
function Sum(xs) / // Precondition: xs is a finite list of numbers / // Postcondition: returns the sum of all elements of xs (0 if xs is empty) / total <- 0 / for each x in xs do / total <- total + x / end for / return total / end function
Exercise
A student writes this loop body for GCD: "a <- b; b <- a mod b". What does it return for GCD(48, 18), and why is it wrong?
Show answer
After a <- 18, the next line computes b <- 18 mod 18 = 0, so the loop ends and returns 18 instead of 6. The second line used the new value of a instead of the old one. Store a mod b in a temporary variable r before overwriting a.