Jump to content

While loop: Difference between revisions

From LabVIEW Wiki
No edit summary
m Logmanoriginal moved page While Loop structure to While loop: Only the first word in a title should start with a capital letter
 
(11 intermediate revisions by 4 users not shown)
Line 1: Line 1:
== What does 0ms wait function do in a while loop ==
{{TOCright}}
If you have multiple loops in your application that don't need to run as fast as possible, then placing a 1ms delay will limit your loops to at most 1000 iterations per second. Without a delay, the loop rate can be in the millions of iterations per second range depending on what is inside it. So that means that your CPU has about 1/1000th as much work to do and can go off and tend to other tasks. If your loop was already taking several ms, then the 1ms delay is likely in parallel and it won't affect the loop speed noticeably. So placing the delay in the loop can drop your CPU usage noticably and allow time for the OS to do other work like send messages that your application may be waiting for, etc.
{{LabVIEW Palette Object Information|palette=Functions Palette/Programming/Structures{{!}}Structures palette|type=structure|icon=Functions Palette - Programming - Structures - While Loop.png}}
[[File:While Loop - Components.png|thumb|Components of a While Loop]]


But what does a wait of 0 ms do? Let's consider the LV execution system to be a xerox copying machine. Everyone that has something to copy heads for the copying machine and lines up. If you have never had the pleasure of waiting at a copy machine, then consider any other time consuming task where people wait in line. Everytime a LV diagram executes a wait function, it is like releasing the copier and staying out of the line for some time delay. After the delay has passed, you will get back in line to make your next copy. A wait of 0 ms is like immediately going to the end of the line to let a smaller copy-task take place. If nobody is in line behind you, you immediately start your next copy task. If someone is in line, it lets them take a turn.
A '''While Loop''' is a primitive [[Functions Palette/Programming/Structures|structure]] that repeats the code within its [[Subdiagram|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.


This 0 ms wait is a pretty cool little trick to make LabVIEW loop parallelism less chunky. It naturally adds some overhead because the loops have some setup time, but when the loops are doing significant work it is tiny. Use it whenever you think you need to, but beware that if some loops don't have any delay, they are still going to
== Usage ==
=== Boolean condition ===


hog the CPU, and the wait of 0 ms may turn into much larger waits because the loops with no waits play by different rules.
[[File:While Loop - Conditional Terminal - Right Click Menu.png|thumb|Conditional terminal right-click menu]]


== Is the while loop really a while loop? ==
A Boolean value must be connected to the conditional terminal [[File:Conditional Terminal - Stop if True.png|frameless|border|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.
No. Strictly speaking the while loop is a "do while" (in C) or repeat until (in PASCAL) programming structure. In other words the loop always executes at least once, and the Boolean test for continuation is done at the end of the loop execution. With a true while loop in a text based program the test is done prior to executing any of the commands contained within the loop.


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.
{| class="wikitable"
! Icon
! Condition
! Description
|-
| [[File:Conditional Terminal - Stop if True.png|frameless|border|Stop if True]]
| Stop if True (default)
| Stops if the Boolean condition is [[File:BooleanTrue2010.png|frameless|border|True]]
|-
| [[File:Conditional Terminal - Continue if True.png|frameless|border|Continue if True]]
| Continue if True
| Stops if the Boolean condition is [[File:BooleanFalse2010.png|frameless|border|False]]
|}


This is of course true, but then there is no real difference between a while loop as you describe it and a for loop in C either. Of course the syntax changes but the functionality is really equivalent.
A loop that never meets its condition is called an [[#Infinite loop|infinite loop]].
And instead of putting the case structure inside the while loop you could just as easily put the while loop in the case structure and get the same behaviour and IMO it looks more clear what is happening.


Also true, but then you loose having a shift register from the loop hold variable(s) that are part of the exit test, unless you then wrap the whole thing in another loop.
=== Infinite loop ===


[QUOTE]
An infinite loop is a loop where the stop condition is never met. The simplest form of infinite loop is achieved by wiring a [[File:BooleanFalse2010.png|frameless|border|False]] constant to the conditional terminal.
and the Boolean test for continuation is done at the end of the loop execution.[/quote]


This is a little misleading, particularly for new [[LabVIEW]] programmers. The boolean test for continuation is done as soon as possible... it only waits until the end of loop execution if there is a data dependency. With no dependacy, the loop will decide whether to execute again before/in parallel with any other code in the loop.
[[File:While Loop - Infinite Loop.png|frameless|border]]


I have seen plenty of people getting one extra iteration than they expected because of this.


[[Category:Uncategorized]]
[[File:While Loop - Infinite Loop - Beginner Mistake 1.png|thumb|A common reason for infinite loops: Wiring Boolean values from outside the 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 [[File:BooleanTrue2010.png|frameless|border|True]] and endlessly if the Boolean value is [[File:BooleanFalse2010.png|frameless|border|False]].
 
=== Execution speed ===
 
While Loops execute as fast as possible and therefore can be a cause for high [[Wikipedia:CPU|CPU]] load. For this reason While Loops should be throttled by using the [[Functions Palette/Programming/Timing/Wait (ms)|Wait (ms)]] function with a delay time that is appropriate for the task.
 
[[File:While Loop - Limit Execution Speed.png|frameless|border]]
 
The delay can be set to <tt>0 ms</tt>, 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 [[Wikipedia:Operating system|operating system]] pauses execution. Use [[Functions Palette/Programming/Structures/Timed Structures/Timed Loop|Timed Loops]] for time-critical tasks.
 
=== Nomenclature controversy ===
 
Strictly speaking the While Loop is a [[Wikipedia:Do while loop|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.
 
== Best practice ==
 
* Use [[Functions Palette/Programming/Structures/For Loop|For Loops]] if the number of iterations is predictable or can be calculated.
* Use [[Functions Palette/Programming/Timing/Wait (ms)|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 ==
{{ambox|text=History information is needed.  What changes have occurred over previous versions?}}
{| class="wikitable"
! Version
! Change(s)
|-
|[[File:LV2018.png|frameless|border|64x64px|LabVIEW 2018|link=LabVIEW 2018]]
|More info to come.
|}
 
== See also ==
* [[WhileLoop class]]
* [[Timed Loop structure|Timed Loops]]
 
== External Links ==
{{ambox|text=Add links to external resources that would also help.}}
 
[[Category:Structures Palette]]
[[Category:While Loop]]

Latest revision as of 15:01, 7 May 2023

Object information
Owning palette(s) Structures palette
Type Structure
Requires Basic Development Environment
Icon
Components of a While Loop

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.

Usage

Boolean condition

Conditional terminal right-click menu

A Boolean value must be connected to the conditional terminal 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.

Icon Condition Description
Stop if True Stop if True (default) Stops if the Boolean condition is True
Continue if True Continue if True Stops if the Boolean condition is False

A loop that never meets its condition is called an infinite loop.

Infinite loop

An infinite loop is a loop where the stop condition is never met. The simplest form of infinite loop is achieved by wiring a False constant to the conditional terminal.


A common reason for infinite loops: Wiring Boolean values from outside the 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 True and endlessly if the Boolean value is False.

Execution speed

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.

Nomenclature controversy

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.

Best practice

  • 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

Version Change(s)
LabVIEW 2018 More info to come.

See also

External Links