State Machine Patterns
Toggle State
A tiny state machine can switch between off and on with each event.
state
An enum names each possible state instead of using unexplained integers.
event
Each button press applies the same transition rule to the current state.
Toggle State
toggle_state.c
Replay: real traced execution (multi-file project)
#include <stdio.h>
enum SwitchState {
SWITCH_OFF,
SWITCH_ON
};
enum SwitchState toggle(enum SwitchState state) {
if (state == SWITCH_OFF) {
return SWITCH_ON;
}
return SWITCH_OFF;
}
int main(void) {
int presses = 3;
enum SwitchState state = SWITCH_OFF;
for (int i = 0; i < presses; i++) {
state = toggle(state);
}
printf("presses=%d state=%d\n", presses, state);
return 0;
}
#include <stdio.h>
enum SwitchState {
SWITCH_OFF,
SWITCH_ON
};
enum SwitchState toggle(enum SwitchState state) {
if (state == SWITCH_OFF) {
return SWITCH_ON;
}
return SWITCH_OFF;
}
int main(void) {
int presses = 1;
enum SwitchState state = SWITCH_OFF;
for (int i = 0; i < presses; i++) {
state = toggle(state);
}
printf("presses=%d state=%d\n", presses, state);
return 0;
}
#include <stdio.h>
enum SwitchState {
SWITCH_OFF,
SWITCH_ON
};
enum SwitchState toggle(enum SwitchState state) {
if (state == SWITCH_OFF) {
return SWITCH_ON;
}
return SWITCH_OFF;
}
int main(void) {
int presses = 4;
enum SwitchState state = SWITCH_OFF;
for (int i = 0; i < presses; i++) {
state = toggle(state);
}
printf("presses=%d state=%d\n", presses, state);
return 0;
}
presses ← 3
15int main(void) {16 int presses→ 3 = 3; //@presses=1, 417 enum SwitchState state = SWITCH_OFF;for (int i = 0; i < presses; i++)
pass 1 of 319for (int i0 = 0; i < presses3; i++) {20 state0 = toggle(state);21}All 3 passes — pass 1 is the card above pass istateSWITCH_OFFSWITCH_ON1 0 0 0 1 2 1 1 — — 3 2 0 0 1 enum SwitchState toggle(enum SwitchState state)
pass 1 of 38enum SwitchState toggle(enum SwitchState state0) {9 if (state == SWITCH_OFF) {All 3 passes — pass 1 is the card above pass stateSWITCH_ON1 0 1 2 1 — 3 0 1 if (state == SWITCH_OFF)
pass 1 of 28enum SwitchState toggle(enum SwitchState state) {9 if (state0 == SWITCH_OFF0) {10 return SWITCH_ON1;11 }state ← 1
19for (int i = 0; i < presses; i++) {20 state→ 1 = toggle(state);21}state ← 0
19for (int i = 0; i < presses; i++) {20 state→ 0 = toggle(state);21}if (state == SWITCH_OFF)
pass 2 of 28enum SwitchState toggle(enum SwitchState state) {9 if (state0 == SWITCH_OFF0) {10 return SWITCH_ON1;11 }state ← 1
19for (int i = 0; i < presses; i++) {20 state→ 1 = toggle(state);21}printf("presses=%d state=%d ", presses, state);
23 printf("presses=%d state=%d\n", presses3, state1);24 return 0;25}outputpresses=3 state=1
presses ← 1
15int main(void) {16 int presses→ 1 = 1;17 enum SwitchState state = SWITCH_OFF;for (int i = 0; i < presses; i++)
19for (int i0 = 0; i < presses1; i++) {20 state0 = toggle(state);21}enum SwitchState toggle(enum SwitchState state)
8enum SwitchState toggle(enum SwitchState state0) {9 if (state == SWITCH_OFF) {if (state == SWITCH_OFF)
8enum SwitchState toggle(enum SwitchState state) {9 if (state0 == SWITCH_OFF0) {10 return SWITCH_ON1;11 }state ← 1
19for (int i = 0; i < presses; i++) {20 state→ 1 = toggle(state);21}printf("presses=%d state=%d ", presses, state);
23 printf("presses=%d state=%d\n", presses1, state1);24 return 0;25}outputpresses=1 state=1
presses ← 4
15int main(void) {16 int presses→ 4 = 4;17 enum SwitchState state = SWITCH_OFF;for (int i = 0; i < presses; i++)
pass 1 of 419for (int i0 = 0; i < presses4; i++) {20 state0 = toggle(state);21}All 4 passes — pass 1 is the card above pass istateSWITCH_OFFSWITCH_ON1 0 0 0 1 2 1 1 — — 3 2 0 0 1 4 3 1 — — enum SwitchState toggle(enum SwitchState state)
pass 1 of 48enum SwitchState toggle(enum SwitchState state0) {9 if (state == SWITCH_OFF) {All 4 passes — pass 1 is the card above pass stateSWITCH_ON1 0 1 2 1 — 3 0 1 4 1 — if (state == SWITCH_OFF)
pass 1 of 28enum SwitchState toggle(enum SwitchState state) {9 if (state0 == SWITCH_OFF0) {10 return SWITCH_ON1;11 }state ← 1
19for (int i = 0; i < presses; i++) {20 state→ 1 = toggle(state);21}state ← 0
19for (int i = 0; i < presses; i++) {20 state→ 0 = toggle(state);21}if (state == SWITCH_OFF)
pass 2 of 28enum SwitchState toggle(enum SwitchState state) {9 if (state0 == SWITCH_OFF0) {10 return SWITCH_ON1;11 }state ← 1
19for (int i = 0; i < presses; i++) {20 state→ 1 = toggle(state);21}state ← 0
19for (int i = 0; i < presses; i++) {20 state→ 0 = toggle(state);21}printf("presses=%d state=%d ", presses, state);
23 printf("presses=%d state=%d\n", presses4, state0);24 return 0;25}outputpresses=4 state=0