LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Stop a while like C does it.

Hello I'm trying to stop a while like C does, I mean, in C or Matlab first you eval the condition of the loop and if it's true you iterate the while, when I try it here in labview, for example stop if iteration >= 10 with a boolean compare between Iteration number plus one (It counter starts in 0) and 10, I always get the 10 iteration done because the labview while first does the code in the while and after evaluates the stop condition. I would like to get it as Matlab, evaluating first the condition and then doing, if ok, the iteration code, does anybody know how to do it? I attach an example, I iterate until <10, so the code should stop in iteration 10 after evaluating the comparation without incrementing the it counter for "Next Value", as C would do. More or less Labview gives me a "Do While" and I want a "While". Best Javier
Download All
0 Kudos
Message 1 of 7
(3,096 Views)

Just wrap the code inside the loop in a case structure.
If the condition is true, execute the T case, otherwise execute the F case.

To learn more about LabVIEW, I suggest you try searching this site and google for LabVIEW tutorials. Here, here, here, here, here and here are a few you can start with and here are some tutorial videos. You can also contact your local NI office and join one of their courses.
In addition, I suggest you read the LabVIEW style guide and the LabVIEW user manual (Help>>Search the LabVIEW Bookshelf).


___________________
Try to take over the world!
0 Kudos
Message 2 of 7
(3,087 Views)
If you really want to do it that way, the only way LabVIEW supports is is for you to put a case statement around the code inside the loop, so when the loop comes back, when you evaluate your fallout condition, you don't do anything.  However, if you are doing anything that results in data getting passed out of the loop, the data will not be valid unless you are using a shift register.

The better approach is to realize the way the while loop works and do something similar to what you have done in your attached example.  Your code runs for 10 iterations.  Youu added one to i so the 10 made sense for you.  The other option is do i<9 if you don't want to do the addition.
0 Kudos
Message 3 of 7
(3,086 Views)

LabVIEW only directly supports the "Do...While" construct with its While loop.  There are fairly simple workarounds though.  One example is to put all your active code inside a boolean Case structure in your While loop.   Let the Loop termination boolean wire drive the case structure so that when the boolean means "terminate", the code doesn't execute.

(There are 2 more possible gotchas associated with this workaround.  If you care about the final value of the iteration terminal 'i', you may find that it's too large and needs to be decremented.  If you have other calculated values to pass out of the loop, you'll need a good way to get them through your case structure.  This will likely mean that you'll use shift registers so that on the final loop iteration, all the left-side-values pass straight through the Case structure to the right-side-values.)

-Kevin P.

 

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
0 Kudos
Message 4 of 7
(3,079 Views)

Personaly i would like Labview to support the "break" command, that C has (in a while loop).

And why not a "return" command to terminate a sub-vi.

They are not necessary, but whould make wiring easier.

0 Kudos
Message 5 of 7
(3,039 Views)


Pnt wrote:

Personaly i would like Labview to support the "break" command, that C has (in a while loop).

And why not a "return" command to terminate a sub-vi.


Both of these have a serious problem -
 
C is linear while LabVIEW is 2 dimensional and runs code in parallel.
That means that when you call break or return in C, you have a well defined behavior, even if it has the potential of introducing bugs.
In LabVIEW, you have no way of knowing which parts of your code did or did not execute when you call the command, so you would get undefined behavior.
 
Additionally, the dataflow nature of LabVIEW means that a command similar to return is not really possible, since LV does not have a return parameter like C does. You have to explicitly decide which value to put on the wire and which pieces of code to execute or not, which is what the use of case structures gives you.

___________________
Try to take over the world!
0 Kudos
Message 6 of 7
(3,026 Views)
To add to tst last comment, the parallelism is the issue that C-while loop is not a straight foreward implementation. In linear programming, you can calculate your break condition before the code or after the code (and also everywhere else), which resulted in 'while' and 'until' statements. In LabView, the break condition is calculated in parallel to the code in the loop (which is pretty cool, if you'd imagine the giga-core computers we will use in the next decades).

There is some easy workarounds not yet suggested: you can iterate the n+1th loop as well, but use the output of the n-th loop, by using a shift register connected to the output of the loop (containing the value of the last iteration) or in case of auto-indexing, you only need to delete the last element of the array.

Felix
0 Kudos
Message 7 of 7
(3,008 Views)