While Loop structure
|Owning palette(s)||Structures palette|
|Requires||Basic Development Environment|
A While Loop is a primitive structure that repeats the code within its subdiagram until a Boolean condition is met. The code within its subdiagram is executed at least once. It runs endlessly if the condition is not met.
A Boolean value must be connected to the conditional terminal of the loop. The condition can be changed at the conditional terminal, via the right-click menu options "Stop if True" and "Continue if True", or by clicking on the terminal.
|Stop if True (default)||Stops if the Boolean condition is|
|Continue if True||Stops if the Boolean condition is|
A loop that never meets its condition is called an infinite loop.
Infinite loops will never stop and can cause a program to be unresponsive to user input. Most cases of infinite loops are not intentional and the result of bugs. A typical mistake is wiring a Boolean value from outside the loop to the conditional terminal. Since input tunnels are evaluated before the first iteration, the Boolean value always stays the same. This causes the While Loop to execute its subdiagram exactly once if the Boolean value is and endlessly if the Boolean value is .
While Loops execute as fast as possible and therefore can be a cause for high CPU load. For this reason While Loops should be throttled by using the Wait (ms) function with a delay time that is appropriate for the task.
The delay can be set to 0 ms, which makes the loop execute as fast as possible without clogging the CPU.
Note that limiting the execution speed does not guarantee that the loop is executed at exact intervals. The loop can still execute slower if the code takes longer to execute, or the operating system pauses execution. Use Timed Loops for time-critical tasks.
Strictly speaking the While Loop is a Do While Loop, because the stop condition is checked at the end of a loop, which requires the subdiagram to execute at least once. A "real" While Loop, however, would first check the stop condition and execute the subdiagram only if the condition is not met. This can be confusing to programmers that are familiar with other programming languages and that are new to LabVIEW.
- Use For Loops if the number of iterations is predictable or can be calculated.
- Use Wait (ms) to limit the execution speed of a While Loop.
Tips and tricks
- Wire an error cluster to the conditional terminal to terminate the loop on an error.
|History information is needed. What changes have occurred over previous versions?|
|More info to come.|
|Add links to external resources that would also help.|