CIE AS & A Level Computer Science, section 12.2 (Program design)
What a state-transition diagram is
A state-transition diagram shows a system as a set of states and the transitions between them, caused by inputs or events. It is used to document an algorithm where the behaviour depends on the current state, such as a machine, a protocol or a game.
Features
- States: circles that show the condition the system is in.
- Transitions: arrows between states, labelled with the input (event) that triggers them, and sometimes the output.
- A start state (with an incoming arrow) and, if needed, a final state.
Worked example: a turnstile
Inputs are coin and push.
| Current state | Input | Next state |
|---|---|---|
| Locked | coin | Unlocked |
| Locked | push | Locked |
| Unlocked | push | Locked |
| Unlocked | coin | Unlocked |
This state table lists every combination.
Python implementation
transitions = {
("Locked", "coin"): "Unlocked",
("Locked", "push"): "Locked",
("Unlocked", "push"): "Locked",
("Unlocked", "coin"): "Unlocked",
}
state = "Locked"
for event in ["push", "coin", "push"]:
state = transitions[(state, event)]
print(event, "->", state) # push -> Locked, coin -> Unlocked, push -> Locked
Benefits
The design is easy to check for missing cases, and makes the system's behaviour clear to developers and users.
Common mistake: leaving a transition out. Every state should have an arrow for every possible input (or a clear rule about what happens when none applies). Also, label the arrows with the input, not with the state name.
Exam tip: draw each state as a circle, label each transition, and mark the start state. Check against a state table that all combinations are covered.
Check yourself
What does a state-transition diagram show?
The states of a system and the transitions between them caused by inputs or events.
In the turnstile, what happens if you push when it is locked?
It stays locked.
What do the arrows in the diagram represent?
Transitions from one state to another, labelled with the event that causes them.