Build and Configure a While Loop in LabVIEW

Updated Oct 26, 2023

Environment

Software

  • LabVIEW

A While Loop is a structure you use to execute a block of code repeatedly until a given condition is met. When the VI runs, the code inside the While Loop executes, and then the conditional terminal is evaluated.

This tutorial walks you through how to build and configure a While Loop to LabVIEW. This introduction material is good for those new to LabVIEW and looking to familiarize themselves with this basic programming structure.

Build a While Loop

  1. Launch LabVIEW and open a new VI. You can open a blank VI by selecting File»New VI
  2. Right-click on the block diagram to display a temporary version of the Functions palette.
    • Optional: You can click the thumbtack in the upper left corner of the Functions palette to pin the palette so it is no longer temporary.
  3. Navigate to Structures and select the While Loop. 
    • To place the loop on the block diagram, left-click and drag the mouse until the loop is your desired size.
  4. Add a Stop button to the front panel. You can find this under Controls Palette»Boolean»Stop.
  5. On the block diagram, drag the Stop button icon into the while loop. 
  6. Wire the Stop button to the conditional terminal so that you can control the execution of the while loop.
    • By default, when the Stop button is pressed, a true value is passed to the conditional terminal causing the while loop to stop execution.
    • Note: Instead of just a Stop button, you can wire any Boolean data to the conditional terminal to control the execution of a while loop.
  1. Configure the conditional terminal. To switch the conditional terminal between Stop if True and Continue if True, right-click on the conditional terminal and select the desired setting.
    • The conditional terminal defines when the loop stops. There are two settings for the conditional terminal: Stop if True () and Continue if True ().
      • If the conditional terminal is set to Stop if True, and a Boolean value of true is sent to the conditional terminal, the loop halts execution. This is the default setting.
      • When set to Continue if True, the while loop runs only if a Boolean value of true is sent to the terminal.
  2. (Optional) Monitor the number of iterations the loop has undergone by right-clicking on the iteration terminal and selecting Create Indicator.
 

Add Structure Tunnels to Your While Loop

Use structure tunnels to feed data into and out of structures like the while loop. If you want to send data into your while loop, you need to create structure tunnels.
  • Data sent into the While Loop is sent only on the first iteration
  • Data sent out of the while loop is sent only after the last iteration.
    • If you want your front panel data to be continuously updated, their indicators should be located inside of the while loop

Example: Determine Iterations Completed

  1. Complete steps to build a while loop (above).
  2. Create a numeric indicator on the front panel (Controls >> Numeric >> Numeric Indicator).
    1. (Optional) To avoid coercion of data types, change the numeric representation to I32 by right-clicking on the indicator and selecting Representation >> I32.
  3. On the block diagram, wire the iteration counter to the numeric indicator icon
    • The structure tunnel appears as a solid block on the border of the while loop. The block is the color of the data type wired to the tunnel.
    • Data passes out of a loop after the loop terminates. 
In the following block diagram, the iteration terminal is connected to a tunnel. The value in the tunnel does not get passed to the iteration indicator until the while loop finishes executing. Bring the snippet into your LabVIEW code and notice:
  • The Random Number indicator is placed inside the while loop. Data for this indicator updates with each iteration.
  • The Iteration Count indicator has a value of zero while the code is running. This is because the tunnel does not pass information to the indicator until the while loop has stopped execution.
  • When you press stop, the iteration count indicator will now update.

 

Setting Stop Conditions

By using Boolean functions, you can implement multiple conditions to affect your while loop conditional terminal. You can use an “or” function to compare an error wire status and a Stop button control so that if either is TRUE, the conditional terminal receives a TRUE signal, and the while loop stops.

  1. Complete steps to build a while loop (above).
  2. Place an Error In 3D.ctl control on the front panel (Controls Palette»Data Containers»Error In 3D.ctl)
    1. On the block diagram, make sure the Error In 3D.ctl icon is outside of the while loop.
  3. Remove the connection wire between the Stop button and the conditional terminal.
  4. Place a Simple Error Handler outside of the while loop (Functions Palette»Dialog & User Interface»Simple Error Handler.vi). This function reports any errors encountered when called.
  5. Link the error in control with the Simple Error Handler through the while loop via structure tunnels.
    1. Wire the output of the error in control to the border of the while loop. Click on the border to create a tunnel.
    2. Continue the error wire from the tunnel to the opposite border of the while loop and into the input of the Simple Error Handler.
  6. Place an Unbundle By Name function inside of the while loop (Functions Palette»Programming»Cluster, Class, & Variant»Unbundle by Name). You can use this function to separate the Boolean portion of the error data wire.
    1. Connect the error wire to the Unbundle by Name function. 
    2. Left-click on the Unbundle function and select status to present the boolean portion of the error data.
  7. Place an or function inside of the while loop (Functions Palette»Programming»Boolean»Or).
  8. Link the data wires.
    1. Into the Or function, wire both the Stop button control and the Unbundle by Name status.
    2. Connect the output of the Or function into the conditional terminal.
The snippet below stops execution of the while loop if either the Stop button is pressed or an error is detected inside of the while loop.
 

Other Stop Condition Considerations

The situation described above (stop on user input or on error) is a very common use case for setting the conditional stop input. But this is not the only way to add complexity into your stop conditions:
  • If you would like to compare three or more Boolean conditions, use the Compound Arithmetic function. This would be useful if you would like to monitor error states, user inputs, and measurement data values simultaneously and stop running code if a threshold or state has occurred. 
  • When comparing error wires using Or, Compound Arithmetic, or other Boolean comparison functions, you can wire the error bundle directly into the arithmetic function. The error status Boolean will automatically be parsed out.
    • This may not be true for all versions of LabVIEW. The process described in Setting Stop Conditions section is applicable in all versions of LabVIEW and a correct process to follow.

Next Steps

Once you have familiarized yourself with the While Loop, learn how you can pass information from one loop iteration to the next using shift registers . You can use the Learn LabVIEW Basics online educational material as a resource to get started.