No history yet

Introduction to State Machines

Systems with Memory

Think about a simple light switch. It can be either on or off. Flicking the switch changes its condition. This is a very basic example of a system with different states. Many systems in the world, both digital and physical, can be described by their current condition and the things that cause them to change.

A state machine is a model of behavior. It describes a system that can be in one of several conditions, or "states," and specifies how the system moves from one state to another.

At any given moment, the system is in a single state. It waits for something to happen, like a button press or a timer finishing. When that event occurs, the system can transition to a new state. This simple model helps us understand and design complex behaviors in a predictable way.

The Building Blocks

Every state machine is built from a few core components that work together to define its behavior.

State

noun

A specific condition or situation that a system can be in. A system can only be in one state at a time.

States represent the memory of the system. For example, a user account might be in an 'active', 'suspended', or 'closed' state.

Transition

noun

The movement from one state to another. Transitions are the pathways that connect the states.

A transition is like a one-way street from a starting state to an ending state. A state can also transition back to itself, which is called a self-transition.

Event

noun

An occurrence that can trigger a state transition. Events are the inputs to the state machine.

Events are what make the machine go. An event could be a user clicking a button, data arriving from a network, or a timer expiring.

Action

noun

An activity or operation that is performed when a transition occurs. Actions are the outputs of the state machine.

Actions are the work that gets done. When a transition from 'payment pending' to 'payment complete' occurs, an action might be to update a database and display a confirmation message.

A Vending Machine Example

Let's look at a familiar system: a vending machine that sells snacks for $1. We can model its basic logic using a state machine. It has a few distinct states it can be in.

Initially, the machine is Idle, waiting for a customer. When a customer inserts money (an event), it transitions to the Has Money state. In this state, it waits for another event: the customer selecting a snack.

Once a snack is selected, the machine performs an action—dispensing the snack—and transitions back to the Idle state to await the next customer. This cycle continues, moving between states based on events and performing actions along the way. More complex vending machines would have more states and transitions, but the core idea is the same.

Lesson image

Why Bother?

Modeling a system as a state machine offers several advantages. First, it makes complex logic easier to understand. By breaking down behavior into a finite number of states and the explicit transitions between them, you can visualize the entire workflow. This clarity helps prevent bugs, because it forces you to consider all possible scenarios.

Second, state machines lead to more predictable and maintainable code. The logic is centralized and follows clear rules, rather than being scattered across various conditional statements. When a new feature is needed, it's often as simple as adding a new state or transition without disturbing the existing logic.

A state machine controls the workflow or flow of execution of tasks.

From managing user interfaces to controlling hardware, state machines provide a robust framework for building systems that behave exactly as expected.

Time to check your understanding.

Quiz Questions 1/5

What is the primary role of a 'state' in a state machine?

Quiz Questions 2/5

In the provided vending machine example, what causes the transition from the Idle state to the Has Money state?

By thinking in terms of states and transitions, you can tackle complex problems with a clear and structured approach.