AS Level · Topic 12.2
A-Level Computer Science 9618 / Topic 12 / AS

Every input, a new state.

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.

The key idea

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.

State-transition diagram for a car alarm DisarmedArmedSounding lock unlock door opened / siren on unlock / siren off start
Fig. 1: the alarm starts Disarmed; locking arms it; opening a door while armed sounds the siren; unlocking always disarms it
Section 01

States and transitions.

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.

Examiner trap

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.

Section 02

Tables and traces.

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 stateInputOutputNext state
DisarmedlocknoneArmed
ArmedunlocknoneDisarmed
Armeddoor openedsiren onSounding
Soundingunlocksiren offDisarmed
0 fails1 fail2 fails LockedOpen wrongwrongwrong correctcorrectcorrect double circle = halting state (no transitions leave it)
Fig. 2: a door keypad allows three wrong PINs; the third wrong PIN locks it permanently
Worked example Trace a sequence
The keypad in Fig. 2 receives the inputs wrong, wrong, correct. Give each state it passes through and the final state.
  1. Start state: 0 fails.
  2. wrong: 0 fails moves to 1 fail.
  3. wrong: 1 fail moves to 2 fails.
  4. correct: 2 fails moves to Open, a halting state.
Answer: 0 fails, 1 fail, 2 fails, Open: the door opens
Your turn
What is the final state of the keypad in Fig. 2 after the inputs wrong, wrong, wrong, correct?
Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

On a state-transition diagram, what does a circle represent?

Quick check+10 XP

In Fig. 1, what is the state after the inputs lock, unlock, lock?

Quick check+10 XP

How is a halting state shown?

Quick check+10 XP

A transition is labelled coin / unlock. What does this mean?

Section 03

From diagram to code.

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
Your turn
Using Fig. 1, what is the state and what is output after the inputs lock, door opened, lock, unlock?
Examiner trap

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.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

What is the purpose of a state-transition diagram?

Exam style+20 XP

In Fig. 2, the keypad receives wrong, correct. What is the final state?

Exam style+20 XP

A state-transition table has the row: Armed, door opened, siren on, Sounding. Which column holds "siren on"?

Exam style+20 XP

When a state-transition diagram is coded in pseudocode, which construct most directly selects what to do for the current state?

Skill unlocked

State-transition diagrams, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
States, transitions, input / output labels, start and halting states, state-transition tables, traces and pseudocode.