What happens, and does code work properly if we write switch-case structure like this, why its not good to write code like this? I’m porting some firmware and have switch-case statement interrupted like this one, and when I remove code between that switch-case statements, it begins to operate normally.
switch(state){
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
}
//here we interrupt switch-case and do a some other code
// after that code we continue with same switch
switch(state){
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
}
1
Sounds like the code in the middle is changing the value of the state variable, giving you different execution in the second switch statement. Presumably, this matters; create a new variable, store the value of state into it and use this in the second switch statement:
int savedState = state ;
switch( savedState ) { ... } // 0, 1, 2, 3
// Other Code
switch( savedState ) { ... } // Everything else