A transition table stores the next state for each state and event pair.

table Rows represent current states and columns represent events.
lookup One table lookup replaces a chain of repeated branch checks.

Transition Table

event
transition_table.c
Replay: real traced execution (multi-file project)
#include <stdio.h>

enum DoorState {
    DOOR_CLOSED,
    DOOR_OPEN,
    DOOR_LOCKED
};

enum DoorEvent {
    EVENT_OPEN,
    EVENT_CLOSE,
    EVENT_LOCK
};

int main(void) {
    int event = EVENT_OPEN;
    int table[3][3] = {
        {DOOR_OPEN, DOOR_CLOSED, DOOR_LOCKED},
        {DOOR_OPEN, DOOR_CLOSED, DOOR_OPEN},
        {DOOR_LOCKED, DOOR_LOCKED, DOOR_LOCKED}
    };

    enum DoorState state = DOOR_CLOSED;
    state = (enum DoorState)table[state][event];

    printf("event=%d state=%d\n", event, state);
    return 0;
}
#include <stdio.h>

enum DoorState {
    DOOR_CLOSED,
    DOOR_OPEN,
    DOOR_LOCKED
};

enum DoorEvent {
    EVENT_OPEN,
    EVENT_CLOSE,
    EVENT_LOCK
};

int main(void) {
    int event = EVENT_CLOSE;
    int table[3][3] = {
        {DOOR_OPEN, DOOR_CLOSED, DOOR_LOCKED},
        {DOOR_OPEN, DOOR_CLOSED, DOOR_OPEN},
        {DOOR_LOCKED, DOOR_LOCKED, DOOR_LOCKED}
    };

    enum DoorState state = DOOR_CLOSED;
    state = (enum DoorState)table[state][event];

    printf("event=%d state=%d\n", event, state);
    return 0;
}
#include <stdio.h>

enum DoorState {
    DOOR_CLOSED,
    DOOR_OPEN,
    DOOR_LOCKED
};

enum DoorEvent {
    EVENT_OPEN,
    EVENT_CLOSE,
    EVENT_LOCK
};

int main(void) {
    int event = EVENT_LOCK;
    int table[3][3] = {
        {DOOR_OPEN, DOOR_CLOSED, DOOR_LOCKED},
        {DOOR_OPEN, DOOR_CLOSED, DOOR_OPEN},
        {DOOR_LOCKED, DOOR_LOCKED, DOOR_LOCKED}
    };

    enum DoorState state = DOOR_CLOSED;
    state = (enum DoorState)table[state][event];

    printf("event=%d state=%d\n", event, state);
    return 0;
}
  1. event ← 0, table ← ⟨addr A⟩, state ← 1

    15int main(void) {16    int event→ 0 = EVENT_OPEN0; //@event=EVENT_CLOSE, EVENT_LOCK17    int table→ ⟨addr A⟩[3][3] = {18        {DOOR_OPEN1, DOOR_CLOSED0, DOOR_LOCKED2},19        {DOOR_OPEN1, DOOR_CLOSED0, DOOR_OPEN},20        {DOOR_LOCKED2, DOOR_LOCKED, DOOR_LOCKED}21    };2223    enum DoorState state = DOOR_CLOSED;24    state→ 1 = (enum DoorState)table[state][event]1;2526    printf("event=%d state=%d\n", event0, state1);27    return 0;28}
    outputevent=0 state=1
  1. event ← 1, table ← ⟨addr A⟩

    15int main(void) {16    int event→ 1 = EVENT_CLOSE1;17    int table→ ⟨addr A⟩[3][3] = {18        {DOOR_OPEN1, DOOR_CLOSED0, DOOR_LOCKED2},19        {DOOR_OPEN1, DOOR_CLOSED0, DOOR_OPEN},20        {DOOR_LOCKED2, DOOR_LOCKED, DOOR_LOCKED}21    };2223    enum DoorState state = DOOR_CLOSED;24    state0 = (enum DoorState)table[state][event]0;2526    printf("event=%d state=%d\n", event1, state0);27    return 0;28}
    outputevent=1 state=0
  1. event ← 2, table ← ⟨addr A⟩, state ← 2

    15int main(void) {16    int event→ 2 = EVENT_LOCK2;17    int table→ ⟨addr A⟩[3][3] = {18        {DOOR_OPEN1, DOOR_CLOSED0, DOOR_LOCKED2},19        {DOOR_OPEN1, DOOR_CLOSED0, DOOR_OPEN},20        {DOOR_LOCKED2, DOOR_LOCKED, DOOR_LOCKED}21    };2223    enum DoorState state = DOOR_CLOSED;24    state→ 2 = (enum DoorState)table[state][event]2;2526    printf("event=%d state=%d\n", event2, state2);27    return 0;28}
    outputevent=2 state=2