12.2.4 Modelling Behaviour with State-Transition Diagrams
Some algorithms respond differently to an event depending on what has already happened. A state-transition diagram documents this behaviour by showing the possible states of the system and the events that cause movement between them.
This representation is particularly useful for controllers, user sessions, games, communication protocols and interfaces whose behaviour depends on their current condition.
By the end of this section, you should be able to:
- Explain why state-transition diagrams are used to document algorithms.
- Distinguish between a state, an input or event, a transition and an output.
- Interpret and construct state-transition tables.
- Interpret and construct state-transition diagrams.
- Explain start states, final states and self-loops.
- Interpret transition labels written as
input | output. - Trace an input sequence through a finite state machine.
- Check whether a proposed model accounts for the required behaviour.
Why Document Behaviour with States?
A normal step-by-step algorithm describes operations in an expected order. However, some systems repeatedly wait for an event and respond according to their current condition.
For example, the same event may have different effects depending on whether a parcel locker is waiting for a code, has already recorded two incorrect attempts or has opened its compartment.
| Design question | How a state model helps |
|---|---|
| What condition is the system currently in? | The active state records the current condition. |
| Which events can occur now? | Outgoing transitions identify relevant events. |
| What happens after an event? | The transition identifies the next state. |
| Does the event produce a response? | An output can be added to the transition label. |
| Can the process finish? | A final state can represent a completed or halted process. |
Finite State Machines
At any instant, the machine is in one current state. When an input is received, the machine uses both the current state and that input to determine what happens next.
A simple description of an FSM is:
Current state + input β next state and optional output
The set of states is finite because the model lists a limited number of possible conditions. This does not mean that the system can process only a limited number of events. It may move between those states many times.
States, Inputs, Transitions and Outputs
| Term | Meaning | Parcel-locker example |
|---|---|---|
| State | A condition in which the system can remain until an event occurs. | TwoFailedAttempts |
| Input or event | Something detected by the system that may cause a response. | validCode |
| Transition | The movement from the current state to a next state. |
TwoFailedAttempts β CompartmentOpen
|
| Output | An action or message produced when a transition occurs. | unlockCompartment |
Choosing suitable states
A state should represent information that affects future behaviour. In the locker example, the number of previous failed attempts matters because a third incorrect code locks the session.
This relevant history can be stored in separate states:
ReadyOneFailedAttemptTwoFailedAttempts
An alternative algorithm might use one state and a counter variable, but the state-transition model makes the changing behaviour explicit.
CompartmentOpen. Events such as enterCode normally belong
on transition arrows.
State-Transition Tables
A state-transition table organises the behaviour before a diagram is drawn. Each row describes one combination of current state and input.
| Current state | Input | Next state | Output |
|---|---|---|---|
| Ready | validCode |
CompartmentOpen | unlockCompartment |
| Ready | invalidCode |
OneFailedAttempt | showRetryMessage |
| OneFailedAttempt | validCode |
CompartmentOpen | unlockCompartment |
| OneFailedAttempt | invalidCode |
TwoFailedAttempts | showFinalWarning |
| TwoFailedAttempts | validCode |
CompartmentOpen | unlockCompartment |
| TwoFailedAttempts | invalidCode |
SessionEnded | lockSession |
| CompartmentOpen | doorClosed |
SessionEnded | confirmCollection |
| CompartmentOpen | reminderTime |
CompartmentOpen | soundReminder |
State-Transition Diagram Notation
| Diagram feature | Meaning |
|---|---|
| State circle | A condition the system may occupy. |
| Labelled arrow | A transition caused by the input or event written on the arrow. |
| Start arrow | The state occupied when the model begins. |
| Double circle | A final or halting state, when the model has one. |
| Arrow returning to the same state | A self-loop: the event is processed without changing state. |
input | output |
The input causes the transition and produces the named output. |
The start state
The start arrow points to the state occupied before the first input is processed.
In the parcel-locker session, the start state is Ready.
The final state
A final state represents a completed or halted process. The parcel-locker session
reaches SessionEnded after a successful collection or after the third
incorrect code.
Not every FSM needs a final state. A controller that operates continuously may keep moving between states until the whole system is switched off.
Self-Loops and State-Preserving Events
A self-loop shows that the event is recognised even though the system does not move to a different state.
When the locker compartment remains open for too long:
CompartmentOpen
+ reminderTime
β CompartmentOpen
+ soundReminder
The reminder is produced, but the condition of the system remains
CompartmentOpen.
Outputs Produced by Transitions
Some state models show only current state, input and next state. Other models also document an output produced as the transition occurs.
The conventional label is:
input | output
| Transition label | Interpretation |
|---|---|
validCode | unlockCompartment |
A valid code causes the state transition and unlocks the compartment. |
invalidCode | showFinalWarning |
An invalid code produces the final warning before another attempt. |
doorClosed | confirmCollection |
Closing the door ends the session and produces confirmation. |
doorClosed | confirmCollection means that the
doorClosed event produces the
confirmCollection output.
Accounting for Relevant State and Input Combinations
A useful model should define what happens for every input that is relevant in each state.
For example, doorClosed is relevant when the compartment is open, but
it may have no meaning while the system is waiting for a code.
| Situation | Design decision |
|---|---|
| An input must change the state. | Draw a transition to a different state. |
| An input is processed but the state remains unchanged. | Draw a self-loop. |
| An input is ignored deliberately. | Document that decision or include a self-loop if the required notation expects every relevant combination. |
| The scenario does not define what happens. | Clarify the requirement rather than guessing silently. |
| Two different transitions exist for the same state and input. | The design is ambiguous unless another condition distinguishes them. |
Worked Example: Parcel-Locker Collection Session
A collection session begins while the locker is waiting for a customer code. A correct code opens the compartment. Three incorrect codes end and lock the session. Once opened, the compartment produces a reminder if it remains open too long. Closing the door completes the session.
Step 1: Identify the states
| State | Meaning |
|---|---|
Ready |
No incorrect code has been entered. |
OneFailedAttempt |
One incorrect code has been entered. |
TwoFailedAttempts |
Two incorrect codes have been entered. |
CompartmentOpen |
The correct compartment is unlocked and open. |
SessionEnded |
The collection succeeded or the session was locked. |
Step 2: Identify the inputs
validCodeinvalidCodedoorClosedreminderTime
Step 3: Identify the outputs
unlockCompartmentshowRetryMessageshowFinalWarninglockSessionsoundReminderconfirmCollection
Step 4: Write the transitions
Ready
validCode | unlockCompartment
β CompartmentOpen
Ready
invalidCode | showRetryMessage
β OneFailedAttempt
OneFailedAttempt
invalidCode | showFinalWarning
β TwoFailedAttempts
TwoFailedAttempts
invalidCode | lockSession
β SessionEnded
CompartmentOpen
reminderTime | soundReminder
β CompartmentOpen
CompartmentOpen
doorClosed | confirmCollection
β SessionEnded
A valid code from either failed-attempt state also moves to
CompartmentOpen and produces
unlockCompartment.
invalidCode changes after each previous failure.
Method: From Scenario to State-Transition Diagram
- Identify changing conditions. Find the conditions that affect how later events are handled.
- Name the states. Use concise names that describe conditions rather than actions.
- Mark the start state. Decide the condition before the first input.
- Identify any final state. Decide whether the model can finish or halt.
- List inputs and events. Include user actions, sensor events and timer events where relevant.
- Build a transition table. Determine the next state for each relevant current-state and input pair.
-
Add outputs.
Use
input | outputwhere the transition produces a response. - Draw the states and transitions. Transfer every table row to the diagram.
- Check self-loops and missing transitions. Confirm what happens when an event does not change the state.
- Trace test sequences. Follow several valid and exceptional paths through the model.
Tracing an Input Sequence
To trace an FSM, begin at the start state and process one input at a time. The next state from one transition becomes the current state for the next input.
Trace A: successful first attempt
| Step | Current state | Input | Output | Next state |
|---|---|---|---|---|
| 1 | Ready | validCode |
unlockCompartment |
CompartmentOpen |
| 2 | CompartmentOpen | doorClosed |
confirmCollection |
SessionEnded |
Trace B: three incorrect codes
| Step | Current state | Input | Output | Next state |
|---|---|---|---|---|
| 1 | Ready | invalidCode |
showRetryMessage |
OneFailedAttempt |
| 2 | OneFailedAttempt | invalidCode |
showFinalWarning |
TwoFailedAttempts |
| 3 | TwoFailedAttempts | invalidCode |
lockSession |
SessionEnded |
Trace C: reminder while open
Ready
β validCode
β CompartmentOpen
β reminderTime
β CompartmentOpen
β doorClosed
β SessionEnded
Checking a State-Transition Model
| Check | Question to ask | Possible warning sign |
|---|---|---|
| State quality | Does each state describe a meaningful condition? | A state is named EnterCode, which is an action. |
| Start state | Is the initial condition shown? | No start arrow is present. |
| Final state | Is a halting condition shown where required? | A completed session still has unexplained outgoing transitions. |
| Transition labels | Does every arrow identify the event that causes it? | An arrow has no input label. |
| Outputs | Are required responses attached to the correct transitions? | unlockCompartment is written as a state. |
| Self-loops | Are state-preserving events represented? | The reminder event is absent from the open state. |
| Completeness | Is every relevant state/input combination addressed? | The model does not say what a valid code does after one failure. |
| Consistency | Do the table and diagram describe the same behaviour? | A table row has no matching arrow. |
Interactive: Parcel-Locker FSM Explorer
Choose events and observe how the current state changes. The active transition, output and transition-table row are highlighted.
Common Mistakes and Misconceptions
- Using events as state names. States describe conditions; events normally label arrows.
- Choosing a next state from the input alone. The current state must also be considered.
- Forgetting the start arrow. The initial state must be identifiable.
- Assuming every machine has a final state. Continuously operating systems may have none.
- Omitting self-loops. An event can be processed without changing state.
- Putting outputs inside state circles. In a Mealy-style diagram, outputs belong on transition labels.
-
Reading
input | outputas two inputs. The value after the bar is the response produced. - Drawing two ambiguous transitions. One current-state and input pair should not produce two unexplained results.
- Failing to model relevant history. If previous failures change later behaviour, that information must be stored in the states or elsewhere in the algorithm.
- Failing to compare the table and diagram. Both representations should describe the same transitions.
Practice
Core questions
- Define the term finite state machine.
- Explain the purpose of a state-transition diagram.
- Distinguish between a state and an input.
- Explain what a labelled transition arrow represents.
- Explain the purpose of the start arrow.
- Explain when a double circle is used.
- Explain the meaning of a self-loop.
-
Explain the transition label
timeout | displayWarning. - Explain why the current state must be considered when interpreting an input.
Scenario A: Greenhouse Vent Controller
A greenhouse vent begins closed. A high-temperature event starts opening it. A sensor reports when it is fully open. When the temperature returns to normal, the vent begins closing. A sensor reports when it is fully closed. An obstruction during closing causes the vent to reopen and sound an alarm.
- Identify the possible states.
- Identify the inputs or events.
- Identify any outputs.
- Construct a state-transition table.
- Draw the state-transition diagram.
- Identify the start state.
- Explain whether a final state is needed.
Scenario B: Workshop Queue Ticket
A queue terminal begins ready. Pressing the request button issues a numbered ticket and changes the terminal to ticket-issued. A duplicate press while the ticket is being printed produces a wait message without changing state. Once printing is complete, the terminal returns to ready.
- Identify the states.
- Identify the self-loop.
- Write all transition labels using
input | output. - Construct the table and diagram.
-
Trace the input sequence:
request, request, printingComplete.
Scenario C: Evaluate a Model
A student models a three-attempt access system using only the states
Waiting, Accepted and Rejected. An
incorrect code always returns to Waiting.
- Explain what information the model fails to preserve.
- Suggest additional states.
- Explain how the third incorrect attempt should differ from the first.
- Redraw the relevant part of the model.
Review
| Prompt | A strong response should include |
|---|---|
| Purpose of a state-transition diagram | To document how an algorithm responds to events in different current states. |
| Finite state machine | A model with a fixed set of possible states and rules for moving between them. |
| State | The current condition of the system. |
| Input or event | Something that may cause a transition or output. |
| Transition | Movement from the current state to the next state. |
| Start state | The state occupied before the first input. |
| Final state | A completed or halting condition, when the model has one. |
| Self-loop | A transition that returns to the same state. |
input | output |
The input causes the transition and produces the output. |
| Tracing | Process each input from the state reached by the preceding transition. |