T01: State Diagram Syntax

5.1.1 What Is a State Diagram?

A state diagram (also called a state machine diagram) models how a system transitions between different states over time.

Unlike:

  • Flowcharts → model process steps

  • Sequence diagrams → model interactions

  • Class diagrams → model structure

State diagrams model:

  • System states

  • Transitions

  • Events triggering transitions

  • Lifecycle behavior

State diagrams are ideal for:

  • Authentication flows

  • Order lifecycle tracking

  • Workflow engines

  • Protocol modeling

  • UI state management

In Mermaid, every state diagram begins with:

Mermaid uses stateDiagram-v2 for modern syntax support.


5.1.2 Basic State Definition

The simplest state diagram:

Explanation:

  • [ * ] represents the start or end node

  • Idle and Processing are states

  • --> represents transitions

The diagram reads:

Start → Idle → Processing → End


5.1.3 Understanding Start and End States

Special notation:

Symbol
Meaning

[*]

Initial state

[*] (as target)

Final state

Example:

This models login lifecycle.


5.1.4 Named States

States are defined simply by referencing them.

Mermaid automatically creates states when referenced.

You do not need explicit declaration unless styling or grouping is required.


5.1.5 Adding Labels to Transitions

Transitions can include event names.

Syntax:

Example:

This represents a document approval workflow.


5.1.6 Self-Transitions

A state can transition to itself.

Full example:

Useful for:

  • Retry logic

  • Polling

  • Looping behavior


5.1.7 Composite (Nested) States

States can contain substates.

Syntax:

Example:

This models:

  • Order as a parent state

  • Internal lifecycle stages

Composite states are powerful for complex systems.


5.1.8 Choice States (Decision Nodes)

Choice nodes represent conditional branching.

Syntax:

Example:

Choice nodes resemble decision diamonds in flowcharts but apply to state logic.


5.1.9 Real-World Example — Payment Lifecycle

This models a real payment processing lifecycle.

State diagrams are particularly strong for lifecycle modeling.


5.1.10 Guard Conditions (Conceptual)

Mermaid allows event labeling, but guard conditions are typically represented inside the label.

Example:

This indicates conditional transition.

Though Mermaid does not enforce formal guard syntax like full UML, event labeling communicates conditions clearly.


5.1.11 Parallel States (Advanced Preview)

Mermaid supports parallel states using -- separators.

Example:

This models:

  • Payment and Shipping progressing independently

Parallel modeling is useful for distributed workflows.


5.1.12 Minimal Valid State Diagram

That is all that is required for a valid state diagram.


5.1.13 Common Beginner Mistakes

  1. Forgetting stateDiagram-v2

  2. Omitting start state

  3. Using flowchart syntax inside state diagrams

  4. Confusing state transitions with message flows

  5. Overcomplicating simple workflows

Incorrect:

Correct:


5.1.14 Refactoring Example (Hands-On)

Start simple:

Now refine with events and lifecycle clarity:

This models a realistic account lifecycle.


5.1.15 Hands-On Exercise

Design a Ticketing System Lifecycle:

States:

  • Created

  • Assigned

  • InProgress

  • Resolved

  • Closed

Requirements:

  • Include start and end

  • Add event labels

  • Add at least one self-transition

  • Include one composite state

Focus on lifecycle correctness.


5.1.16 Best Practices

  • Use state diagrams for lifecycle modeling only

  • Keep transitions event-driven

  • Avoid modeling algorithm steps

  • Use composite states for complexity

  • Keep naming consistent

Remember:

State diagrams represent how an entity evolves over time, not how processes execute step-by-step.


5.1.17 Key Takeaways

State diagram syntax includes:

  • stateDiagram-v2 declaration

  • [ * ] start/end states

  • --> transitions

  • Labeled events

  • Composite states

  • Choice states

State diagrams are powerful for modeling:

  • Order flows

  • Payment lifecycles

  • Account states

  • Protocol behavior

  • UI state machines

In the next topic, we will explore Transitions and Composite States in greater depth, including advanced modeling patterns and structuring complex state machines.

Last updated