From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Implementing a test sequence without using GOTO statements

Solved!
Go to solution

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 , 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

-----------------------------------------------------------------------------------------------------------------------------
Waiting For the inner calling 🙂


0 Kudos
Message 1 of 5
(3,105 Views)
Solution
Accepted by topic author djac91

Hello

I have diferent option depends of the number of steps in your secuence.

  1. First:
    while(B)  //
    {
        D;
        C;
    }
    also a "for()", but incluying a "break" for end
  2. Second:   if you have more steps and this steps have jump,  the solution is a states machine

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..

Message 2 of 5
(3,087 Views)

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,

S. Eren BALCI
IMESTEK
Message 3 of 5
(3,053 Views)

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

 

-----------------------------------------------------------------------------------------------------------------------------
Waiting For the inner calling 🙂


0 Kudos
Message 4 of 5
(3,023 Views)

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 .



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 5 of 5
(2,993 Views)