Table Of Contents

Repeating Operations until a Condition Occurs

Last Modified: November 1, 2016

In some situations, you might know that you want to repeat an operation, but you do not know exactly how many times you want to repeat that operation. Instead, you know only that the operation should repeat until a certain condition occurs. For example, you might have a program you want to run repeatedly until a user clicks a stop button or until the code inside the loop produces a particular value.

Use a While Loop to repeat code until a specified condition is met. A While Loop behaves similarly to a do while loop in other programming languages.

What to Use

While Loop

What to Do

Create the following diagram to repeat an operation until a condition occurs.

Customize the gray sections for your unique programming goals.

Place the code you want to repeat on the subdiagram of a While Loop.

To specify the condition under which the loop stops, create code that produces a True Boolean value when the desired stop condition occurs and wire that Boolean value to the condition terminal. By default, the condition terminal is configured to Stop if True. To stop the loop for a False Boolean value instead of a True value, right-click the condition terminal and select Continue if True.

You can also specify when the loop stops by wiring an error cluster to the condition terminal. In this situation, LabVIEW uses the Boolean value of the status of the error to determine when the loop stops.

If you want to know how many loop iterations executed before the stop condition occurred, use the iteration terminal to return the current loop iteration count. The iteration terminal is zero-indexed, meaning it ranges from 0 to n -1. Use Increment to obtain the true number of loop iterations.

spd-note-note
Note  

A While Loop always executes at least one time.

If you want to collect the result of each loop iteration in an array, use an auto-indexing output tunnel to pass values out of the loop. To enable auto-indexing for an output tunnel, right-click the tunnel and select Append Mode»Auto Index Values. If you do not enable auto-indexing, the loop returns only the value from the last loop iteration.

Troubleshooting

If the execution speed of the loop is too fast, place a Wait node on the subdiagram of the loop to specify a wait period.

Examples

Search LabVIEW for the following installed lesson: While Loops


Recently Viewed Topics