From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

infinite for loop

Hello Everybody,

I think it should be possible to create a infinite for loop in LabVIEW!?

You ask why? Well, I think as a C developer it is usual to create an infinite loop with a loop with the condition on the beginning ( while(1) or for( ; ; ) ), not on the end ( do-while(1) ). To be strict, it is a difference if the loop condition stands at the beginning or on the end because the first has minimum passes 0 and the second 1.

The second reason is the perceptibility. I think it is easier to recognize a infinite for loop than a infinite while loop in LabVIEW. I use the infinite while loop often in my LV applications but they are usually big because they have the whole program inside and you must search long for the "Stop if TRUE" connected to the constant "FALSE".

My suggestion is that you can right-click on a for loop, set the infinite Flag (NEW), and the "N" changes to an infinite symbol and the "i" disappears. Maybe the colour can change from blue to green...

And maybe we can do the same thing with the while loop with right-click, set infinite, border colour changes from grey to black, the "i" and the "Stop if TRUE" disappears...

It is just a suggestion, think over it. I think it would be a nice improvement.


Best Regards,

Michael Winkler


PS: Is anybody out there, who needs the loop iterator ("i") in an infinite loop?

Message 1 of 10
(6,978 Views)

Remember that the underlining code for a for loop and a while loop is the same its just a section of code which uses a conditional jump to the lableed start and repeats until the condition is false/true (jump on zero, jump on not zero).  The C language specifications is one of the most flexable semantics and allows many implimentations of the same compiled code.  The only reason for an infinite while loop I have found is in embedded programming (microcontrollers) where the application runs indefinitly.  In labview I only use an infinite loop for debugging purposes and early implementations where a force quit is not dangerous.  A conditional stop is almost always a better solution, so an elegant and safe shutdown procedure can be added (ie close all resources, shutdown atached equiptment to safe states ...).  As for the i counter in an infinite loop it can be very nice to have for performing periodic actions such as i=0 for an initialization code or i%n = zero to do error checking or time consuming operations such as file i/o or GUI updates.  Hope this helps.  ***One thing I do think is that the i indicator should be optional in the while loop.  It seems confusing and (all be it very slightly) inefficient to have an index when one is not used (it adds i++ code to each itteration, although this can be optimized out by a compiler I dont know if it actually is). 

 

Paul

Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
Message 2 of 10
(6,961 Views)

Hi

Although I know the construct, I never had to use it up to now and I also don't know where it is helpful. In my oppinion you will have to terminate each loop once.

Compared to text languages (like C or Java) there is not a break keyword (or better keyvi) available, so there is no way to stop an infinite loop.

For me (and that's really just my oppinion) an infinite loop is not good programming. Ok - I don't know how it is on HW-systems (FPGAs or microprocessors) with no user interface, as I just work on PCs, but on that target I see no use of an infinite loop.

Using LV8.0
--------------------------------------------------------------------
Don't be afraid to rate a good answer... 😉
--------------------------------------------------------------------
Message 3 of 10
(6,954 Views)
Hi Michael,

"PS: Is anybody out there, who needs the loop iterator ("i") in an infinite loop?"

Is anybody out there, who needs an infinite loop?
And why do think of number of passes (minimum zero or one) when you need infinite loops? Why do you care about when the loop condition is evaluated, when the loop runs infinitely?

I agree with falkpl that you only need an infinite loop for embedded devices. So just pick a while loop and wire a constant to the stop condition...

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 4 of 10
(6,951 Views)
Not to try to take the thread in another direction...

But why the '1 star rating' for Michael?  As he said "it is just a suggestion, think over it."

Why does that deserve one star?  The provided responses might provide him with reasons why that does not exist, or why there is a general disagreement with his suggestion, but it was just that: a suggestion to be thought over.

Just my 2 cents.
Message 5 of 10
(6,928 Views)
Don't know about his rating but someone gave all of the respondents one star ratings as well. I don't think disagreeing with an idea is worth one star either. I gave each 5 stars to try to offset that.
Message 6 of 10
(6,920 Views)

@Michael Winkler wrote:
Well, I think as a C developer it is usual to create an infinite loop with a loop with the condition on the beginning ( while(1) or for( ; ; ) ), not on the end ( do-while(1) ). To be strict, it is a difference if the loop condition stands at the beginning or on the end because the first has minimum passes 0 and the second 1.
As was said earlier, it seems strange to talk about infinite looping when you are concerned about the loop termination conditions.

There is a bit in the LabVIEW FAQ about that:

To create a "real" while loop wire the output of your terminating condition to a case structure surrounding the rest of the code within the loop.
Message 7 of 10
(6,914 Views)

Michael,

I don't see much need for an infinite for loop either but please go to http://digital.ni.com/applications/psc.nsf/default?OpenForm&temp1=&node= and make your suggestion. You never know what will happen.Smiley Very Happy

Also, check out the discussion on a new for loop with break at http://forums.ni.com/ni/board?board.id=features.

Message 8 of 10
(6,891 Views)

A point on my suggestion about making the i terminal optional; is this a good idea?  There are many times that I do not use an index in my loop code, it would be nice to turn it off when not used (ie a right click on the loop and have a no indexing option)  This can speed up simple processing loops such as the loop while(not done){value = value+new_value;}, an i++ would double the computational time.  Does any one know if this code is optimized out if the i terminal is not wired?  If this optimization is not used it should be done by labview for the while loops.  Removing the index is not possible ort the for loop since the index is the criteria for a termination.  I have also struggled with the do break option in the loop there have been a few instances where I had to fake it with a case structure inside of the loop (ie on berak dont execute the code, still not as efficient as a break but not too bad). Other than broken data flow why was the break structure omitted from the G specifications?   I think these questions dont deserve 1 star, since it gets us all thinking about labview at an syntatical level, and helps understand the strengths and weaknesses of labview as a programming language. 

 

Paul

Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
Message 9 of 10
(6,888 Views)

Infinite loops are very much part and parcel of embedded programming. 

0 Kudos
Message 10 of 10
(4,770 Views)