Computer Science ยท Lesson 14
State Machines
A state machine is one of computing's most powerful ideas: a system is always in exactly one "state," and events cause it to move to a new state. Traffic lights, vending machines, video game characters, and TCP/IP connections all work this way.
Turing, Mealy, and Moore
Alan Turing (1912โ1954) laid the theoretical foundation for all computing with his 1936 paper "On Computable Numbers," introducing the abstract Turing machine โ essentially a state machine with infinite memory. In 1955โ56, two engineers independently extended his ideas for practical circuits. George Mealy described machines whose outputs depend on both the current state and the current input. Edward Moore described machines whose outputs depend only on the current state. These three models โ Turing, Mealy, and Moore โ underpin every digital device built since: from traffic controllers and elevator logic to compilers, CPUs, and network protocols.
What Is a State Machine?
A finite state machine (FSM) has four key ingredients:
Inputs / Events โ things that can happen (button press, timer, signal)
Transitions โ rules: given current state + input โ move to next state
Start state โ where the machine begins
The critical rule: the system is always in exactly one state at a time. An event fires, a transition runs, and the system moves to the next state โ never in two places at once.
Traffic Light Example
A traffic light cycles through three states, with each state transitioning to the next on a timer event:
Only three states. Only one active at a time. The transitions are completely deterministic โ given the current state and the event, the next state is always exactly predictable.
State Machine Simulator
Choose a demo from the panel. Interact with the action buttons to fire events and watch state transitions happen in real time.
Real-World Applications
Practice Problems
Easy1. A finite state machine can be in multiple states at the same time. True or False?
Easy2. In the traffic light state machine described in the lesson, how many distinct states are there?
Medium3. A vending machine has states: Idle โ Coin Inserted โ Dispensing โ Idle. What event moves the machine from "Coin Inserted" to "Dispensing"?
Medium4. What is the minimum number of transitions needed for a 3-state machine to visit all states in a loop and return to the start?
Challenge5. A combination door lock needs a 3-digit code (digits 0โ9). To model this minimally, how many states does the FSM need? (Count: Start, after digit 1, after digit 2, after digit 3/Unlocked, plus one Error/Reset state)
Innovator Profile ยท See Also