This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

State machine in C problem

Dear all experts

I have to implement a state machine on my project using switch case statement look like this.

//-----------------------------------------
switch(state) {
    case STATE1:
    ...
    state=STATE2;
    break;
  case STATE2:
    ...
    state=STATE4;
    break;
  case STATE3:
    ...
    state=STATE1;
    break;
  case STATE4:
    ...
    state=STATE3;
    break;
}


Later I would like to add some conditions to change direction of the way it goes by omitting 'break' and adding some 'if' statements to take effect, then the code will look like this.

//-----------------------------------------
switch(state) {
  case STATE1:
    ...
if(condition 1) {
    state=STATE2;
  case STATE2:
}
    ...
    state=STATE4;
  case STATE3:
    ...
if(condition 2) {
    state=STATE1;
  case STATE4:
}
    ...
    state=STATE3;
}


If any of you have ever coded up a state machine in C before. Can you suggest me whether it will given a result in compilation error or run time error or not?

Another question is how is the different between the state machine using function pointer and this ordinary switch case statement in term of performance, amount of code, access time latency...etc? : )

Many thanks.

Parents Reply Children
No data