Some programs do not run from start to finish: they wait, react to an event, and wait again. A car alarm, a lift or a login screen behaves differently depending on what has already happened. A state-transition diagram documents exactly how such a system moves from one state to another.
A state-transition diagram documents an algorithm or system whose behaviour depends on its current state and the next input (event). Each state is a circle. Each transition is an arrow from one state to another, labelled with the input that causes it and, after a slash, any output it produces: input / output. An arrow from a dot marks the start state; a double circle marks a halting (final) state. The same information can be written as a state-transition table.
Follow Fig. 1 with your finger. Start at the dot: the alarm is Disarmed. Input "lock" moves it to Armed. Now "door opened" moves it to Sounding and outputs "siren on". Only "unlock" gets it back to Disarmed.
door opened / siren on means: on input door opened, output siren on and move along the arrow.Label every arrow with the input that causes it. An arrow with no label, or a state with no way out when the system should continue, loses marks.
A state-transition table lists one row for each arrow. To trace a diagram, start in the start state and follow one arrow for each input in turn.
| Current state | Input | Output | Next state |
|---|---|---|---|
| Disarmed | lock | none | Armed |
| Armed | unlock | none | Disarmed |
| Armed | door opened | siren on | Sounding |
| Sounding | unlock | siren off | Disarmed |
Four quick checks. Each correct answer earns XP and records this skill on your profile.
On a state-transition diagram, what does a circle represent?
In Fig. 1, what is the state after the inputs lock, unlock, lock?
How is a halting state shown?
A transition is labelled coin / unlock. What does this mean?
A state-transition diagram converts directly into pseudocode. Store the current state in a variable; for each input, a CASE statement on the state decides which transition (if any) to make.
DECLARE State : STRING
State ← "Disarmed"
PROCEDURE HandleEvent(Event : STRING)
CASE OF State
"Disarmed" : IF Event = "lock" THEN
State ← "Armed"
ENDIF
"Armed" : IF Event = "unlock" THEN
State ← "Disarmed"
ELSE
IF Event = "door opened" THEN
State ← "Sounding"
CALL SirenOn()
ENDIF
ENDIF
"Sounding" : IF Event = "unlock" THEN
State ← "Disarmed"
CALL SirenOff()
ENDIF
ENDCASE
ENDPROCEDURE
Do not confuse a state-transition diagram with a flowchart. A flowchart shows the steps of a process in order; a state-transition diagram shows states and the inputs that move the system between them.
Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.
What is the purpose of a state-transition diagram?
In Fig. 2, the keypad receives wrong, correct. What is the final state?
A state-transition table has the row: Armed, door opened, siren on, Sounding. Which column holds "siren on"?
When a state-transition diagram is coded in pseudocode, which construct most directly selects what to do for the current state?
This skill is now recorded on your profile. Keep the chain going.