Showing results for 
Search instead for 
Did you mean: 

[Discussion] Alternative Method for "Preventing Code Execution in the First While Loop Iteration"


Hi there, 


(Please let me know if this post is duplicated)


I was looking for possible way to implement a While-Do loop from text programming language in LabVIEW, and I understand that the While Loop in LabVIEW is similar to a Do-While loop (Running the code in the loop first before checking the condition). 


Here's a simple scenario: 

I was to translate the following code in LabVIEW: 



while (x > 0) {
   // Loop Code goes here
// Note: Bear in mind the above line is just for simple scenario.
// There are cases where code within the loop involves calculate x value }


While I already have an idea in how to implement this in LabVIEW, I came across a LabVIEW Help link (along with other forum posts with solution) that suggested using Case Structure inside a While Loop.

Which means the code will be something like this: 


Although it is not wrong, but I believe this can be inefficient especially when running lots of iterations and execution speed is a concern. The While Loop will always running the Case Structure for every iteration and may introduce some overhead. 


So as alternative, instead of placing the Case Structure inside a While Loop, how about placing the While Loop inside a Case Strcuture? 


With that you can run the Case Structure only once to check the condition first, before running the While Loop if the condition is true. 


After tried running both methods, it seems that the alternative method runs faster than the method described in the LabVIEW Help site. 

I've attached the code below so that you can give it a try. 


Should you have any comments or a much better alternative to implement a While-Do loop in LabVIEW, feel free to discuss and share with us. 


Thank you. 



Ong Ee Lim

See that button on the left side of this post...

If you feel my post is helpful, all you need is just (at most) 2 seconds to click that button, to show your appreciation. Thank you~~

0 Kudos
Message 1 of 3

There are many things that are often balanced when dealing with optimizing code.  In this case, you have the ease of editing and code simplicity versus execution speed.


What happens if the run condition is changed?  You have to update the code in two places.  Is that really worth the very small gain you will have by not having the case structure inside of the loop?  For most applications, I would say not.  Only when I really need to tweak every bit of performance out of a loop would I do somthing like this.  And even then, there are many many more optimizations I could do before this.


With that said, I would really like to see some real benchmarks.  That would better quantify the performance gain.

There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
0 Kudos
Message 2 of 3

LabVIEW has apparently influenced the way I program over the years, and changed how I think about these types of problems.  As a result I've never thought I needed the other type of while loop, and I'm struggling to come up with a real world time that I would have used it recently, had the feature somehow existed in LabVIEW.

Unofficial Forum Rules and Guidelines - Hooovahh - LabVIEW Overlord
Interesting in learning all you can about automotive CAN bus communication? Checkout my 12 part CAN Blog series.
Checkout and help contribute to the community driven LabVIEW Wiki.

Message 3 of 3