03-09-2017 12:47 PM
Hello,
Being relatively new to LabWindows CVI or C as such( at professional level), i am confused whether or not to avoid goto statements in the code. I have often read that it is not a good practice to use a goto statements excessively and using it to jump backward.
I have a test sequence which is in the form of a flow chart, where there are various types of condition checks and associated branching. I couldn't figure out a way to implement the algorithm without using goto statements. I would like to know, what would be an ideal way to do this without use of 'goto'.
For example consider the flow chart shown below.
After process C , the program should loop back to B.
Can someone guide me on this? or is it ok to use goto statement in this scenario?
Best Regards
Solved! Go to Solution.
03-09-2017 01:44 PM
Hello
I have diferent option depends of the number of steps in your secuence.
while (state != no_end){
switch (state)
{
case A: // code for state A
// next state
break;
case B: // code for state B
state = end;
break;
case C: // code for state C
// next state
break;
case 😧 // code for state D
// next state
break;
}
}
Regards. Jose Angel..
03-13-2017 03:16 AM
Hi,
Josse already mentioned it, best way of implementing a flow in C is using switch-case model.
It is very neat and easiliy maintained. Each state is separated in case statments so you do not get lost in the code. You can immediately find the relative part and it is easy to code because you simply follow the same original train of thought which also helped you to draw the flowchart.
Goto is somewhat inevitable when you try to implement a consistent error-handling within CVI. That's the only point I let myself to use goto's. Otherwise I do not use them.
Regards,
03-16-2017 07:51 AM
Hi,
Thanks for your suggestion Josse and Ebalci. I believe this could be the solution.
But, are there performance caveats while using switch cases in implementing state machine when the number of cases are large. The following webpage talks about this and suggests use of function pointers(instead of switch case) to realise state machine in C.
Here is the link to the webpage i'm referring to.
http://codeandlife.com/2013/10/06/tutorial-state-machines-with-c-callbacks/
Which one is more suitable while implementing state machine, switch cases or function pointers.?
Best Regards
Deepu Jacob
03-20-2017 02:36 AM
I just came across this discussion: as of my experience you are not likely to come into performance issues in implementing complex state machines with a switch statement, at least until your scenario is a PC. The caveat the article you are referring to seems more aimed to microcontrollers, with processors in the MHz range of frequency, rather that modern PC in the GHz range!
Code optimization must not be neglected in good programming, but there may be more severe issues that the efficiency of switch statement where to work on in search of a better execution speed, e.g. communications lags, data acquisition devices throughput, file I/O methods and so on .