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.
Create the following diagram to repeat an operation until a condition occurs.
|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, the Boolean value of the status of the error is used 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.
|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 . If you do not enable auto-indexing, the loop returns only the value from the last loop iteration.|
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.