A state machine is essentially a while loop that runs continuously with a case structure that executes different code for a variety of "states". See the picture below:



This program will start in the "Init" state, then begin alternating between the "State 1" and "State 2" states infinitely. You can imagine using this design pattern to produce more complex programs by adding additional states and logic to determine the next state. You could add any code you like to each individual state.
0 Nhận xét