Table Of Contents

State Diagrams

Last Modified: September 27, 2017

To keep track of every state and interaction in your program, design a state diagram of your state machine before you start programming.

A state diagram is an illustration of all the states in a state machine and how these states interact with each other. Each circle represents a state, and each arrow represents a possible state transition. While you create your code, you can follow the diagram flowchart to recall how to structure the state machine and how the states within the machine interact.

The following image shows an example of a basic state diagram for a program that changes the temperature of a room over time.

The state diagram shows all possible state transitions in the program. You can use the state diagram to understand how the states within the program interact before analyzing the code that defines the interactions.

  • Each arrow points toward the endpoint of a state transition. For example, the initialize state can only transition to the adjust temperature state. On the other hand, the program can transition back and forth between the adjust temperature state and wait for event state.
  • A circular arrow, seen on the adjust temperature and wait for event states, indicates that the state can transition to itself.
  • A blue arrow indicates that a transition occurs because of a user action, and a yellow arrow indicates that a transition occurs because of an error in the program.

Recently Viewed Topics