LabVIEW Idea Exchange

cancel
Showing results for 
Search instead for 
Did you mean: 
Mark_Yedinak

Allow While loops to either test the condition before or after the code execution

Forgive me if this is a duplicate idea. I searched and didn't find anything that looked like it.

 

In most text programming languages the While loop comes in two flavors. The do {} while () and the while () {}. The difference is when the loop condition is tested. LabVIEW's current While loop will always execute one time before the condition is evaluated (at least before deciding if the loop should stop). It would be nice if LabVIEW had a While loop that would test the condition BEFORE the code is executed. Think about iterating over a string for example and consuming it in each iteration. You may want to run the loop while the string is not empty. Using the current while loop you would have to test the string and place the loop in a case statement to avoid operating on s string which is empty to begin with. This new loop construct would allow us to avoid wrapping while loops in case structure where we need to test a condition before anything is executed.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
7 Comments
RavensFan
Knight of NI

I agree with you here.  The only catch is that while text based programs would just skip over the section and do nothing if the test fails, LabVIEW always has to return some data value on the output tunnels.  So the structure would have to behave like a 0 iteration For Loop.  Do we pass default data out of the tunnels, or use a shift register to pass through data that originated before the loop?

 

I'd also say there are two more flavors of loops, the Do Until () {} End Loop and the Do{} Loop Until (), but that is just the inverse logic of a Do While and is already handled by the Loop termination being Stop when True vs. Continue when True.

altenbach
Knight of NI

You migh also read the discussion of this related idea.

AristosQueue (NI)
NI Employee (retired)

As far as I'm concerned, this is a duplicate of the idea that altenbach cited. And it is infeasible for exactly the reason I described in that idea. One way or another, you're going to have a second structure somewhere. The best LV could do is introduce a split in the While Loop, but it wouldn't be any different than a case structure. That's just the way data flow works.

G-Money
NI Employee (retired)
Anandjk
Member

Can anybody tell me Why we need an conditional for loop even when we have while loop which works on condition?

wiebe@CARYA
Knight of NI

>Can anybody tell me Why we need an conditional for loop even when we have while loop which works on condition?

 

We don't need it, we've done without them for 15 years. But it is very convenient.

 

A for loop stops when N is reached OR when the condition is met. N can be 0, a while loop will always loop once. This saves a lot of boilerplate code...

 

Intaris
Proven Zealot

 A conditional for loop is bounded which allows some small but elegant compiler optimisations with regards to indexing tunnels.