17 Algorithm Representation: Pseudocode & Struktogramme
Last week we described the GCD algorithm in three ways: natural language, a trace table, and Python code.
Today we add two more notations:
- Pseudocode — precise, structured, language-independent text
- Struktogramme (Nassi–Shneiderman diagrams) — visual block diagrams
By the end of this notebook you will be able to translate freely between Python and pseudocode.
17.1 What Is Pseudocode?
Pseudocode is a way of describing an algorithm using structured, human-readable language that resembles a programming language but is not tied to any specific one.
| Advantage | Explanation |
|---|---|
| More precise than natural language | Avoids ambiguity |
| More readable than real code | No syntax details (colons, brackets, imports) |
| Language-independent | Anyone who knows any language can read it |
| Widely used | Textbooks, exams, professional documentation |
17.2 Pseudocode Conventions
There is no single “official” pseudocode standard, but the conventions below are widely used and will be our standard for this course.
| Element | Python | Pseudocode |
|---|---|---|
| Assignment | x = 5 |
x ← 5 |
| Input | x = int(input()) |
READ x |
| Output | print(x) |
PRINT x |
| Condition | if x > 0: |
IF x > 0 THEN |
| Else | else: |
ELSE |
| End block | (indentation) | END IF |
| While loop | while x > 0: |
WHILE x > 0 DO |
| End loop | (indentation) | END WHILE |
| For loop | for i in range(n): |
FOR i ← 0 TO n-1 DO |
| End for | (indentation) | END FOR |
| Function | def gcd(a, b): |
FUNCTION gcd(a, b) |
| Return | return a |
RETURN a |
| Not equal | != |
≠ |
| Modulo | % |
MOD |
Key idea: Python uses indentation to show structure. Pseudocode uses keywords (
THEN,DO,END IF,END WHILE) to show where blocks begin and end.
17.3 Worked Example: GCD in Pseudocode
Here is the GCD function you wrote last week in Python:
And here is the same algorithm in pseudocode:
FUNCTION gcd(a, b)
WHILE b ≠ 0 DO
r ← a MOD b
a ← b
b ← r
END WHILE
RETURN a
END FUNCTION
Notice the correspondences: - def → FUNCTION … END FUNCTION - while → WHILE … DO … END WHILE - = (assignment) → ← - % → MOD - != → ≠
17.4 Exercise 1: Python → Pseudocode
Read the Python function below carefully. Then write the equivalent pseudocode on paper (or in a new markdown cell).
Use the conventions table above as a reference.
Click to reveal the solution
FUNCTION max_of_three(a, b, c)
IF a ≥ b AND a ≥ c THEN
RETURN a
ELSE IF b ≥ a AND b ≥ c THEN
RETURN b
ELSE
RETURN c
END IF
END FUNCTION
17.5 Exercise 2: Pseudocode → Python
The pseudocode below describes a linear search algorithm. It searches through a list for a given value and returns the index of the first occurrence, or -1 if the value is not found.
FUNCTION linear_search(lst, target)
FOR i ← 0 TO length(lst) - 1 DO
IF lst[i] = target THEN
RETURN i
END IF
END FOR
RETURN -1
END FUNCTION
Task: Implement this algorithm in Python in the cell below.
17.6 Exercise 3: Pseudocode → Python
The pseudocode below prints every integer from 1 to n. For multiples of 3 it prints "Fizz" instead of the number, for multiples of 5 it prints "Buzz", and for multiples of both 3 and 5 it prints "FizzBuzz".
FUNCTION fizzbuzz(n)
FOR i ← 1 TO n DO
IF i MOD 3 = 0 AND i MOD 5 = 0 THEN
PRINT "FizzBuzz"
ELSE IF i MOD 3 = 0 THEN
PRINT "Fizz"
ELSE IF i MOD 5 = 0 THEN
PRINT "Buzz"
ELSE
PRINT i
END IF
END FOR
END FUNCTION
Task: Implement this algorithm in Python.
17.7 Structure Diagrams (Struktogramme)
A Struktogramm (Nassi–Shneiderman diagram) is a visual way of representing an algorithm using nested rectangles. Each rectangle represents one instruction or control structure.
| Advantage | Explanation |
|---|---|
| Visual | Structure is visible at a glance |
| No arrows | Forces structured programming (no “goto”) |
| Widely used | Common in German-speaking CS education and exams |
17.7.1 The Three Building Blocks
Every Struktogramm is built from just three elements:
17.7.1.1 1. Sequence
Instructions are executed one after another, top to bottom.
┌─────────────────────────┐
│ Instruction 1 │
├─────────────────────────┤
│ Instruction 2 │
├─────────────────────────┤
│ Instruction 3 │
└─────────────────────────┘
17.7.1.2 2. Selection (IF / ELSE)
A condition splits execution into two branches.
┌─────────────────────────┐
│ condition? │
├────────────┬────────────┤
│ TRUE │ FALSE │
│ block │ block │
└────────────┴────────────┘
17.7.1.3 3. Iteration (WHILE loop)
A block is repeated as long as a condition holds.
┌─────────────────────────┐
│ WHILE condition │
│ ┌──────────────────────┤
│ │ loop body │
│ └──────────────────────┤
└─────────────────────────┘
17.7.2 Worked Example: GCD Struktogramm
┌──────────────────────────────┐
│ READ a, b │
├──────────────────────────────┤
│ WHILE b ≠ 0 │
│ ┌───────────────────────────┤
│ │ r ← a MOD b │
│ ├───────────────────────────┤
│ │ a ← b │
│ ├───────────────────────────┤
│ │ b ← r │
│ └───────────────────────────┤
├──────────────────────────────┤
│ PRINT a │
└──────────────────────────────┘
Reading the diagram: - Top to bottom = execution order (sequence) - The indented box = the loop body (iteration) - READ at the top = input, PRINT at the bottom = output
17.7.3 Drawing Task (on Paper)
Now draw Struktogramme on paper for the algorithms you translated above:
max_of_three(a, b, c)— uses sequence and selection.linear_search(lst, target)— uses sequence, selection, and iteration.- Sum of all integers from 1 to n.
Compare your diagrams with a partner when you’re done.
17.8 Summary: Comparing Notations
| Notation | Precise? | Executable? | Visual? | Language-independent? |
|---|---|---|---|---|
| Natural language | ✗ | ✗ | ✗ | ✓ |
| Pseudocode | ✓ | ✗ | ✗ | ✓ |
| Struktogramm | ✓ | ✗ | ✓ | ✓ |
| Python | ✓ | ✓ | ✗ | ✗ |
No single notation is “best” — each has strengths. Good computer scientists can switch between notations fluently.