LabVIEW For Loops and While Loops Explained

Panoramica

This article explores some of the basic functions and uses of For Loops and While Loops and the appropriate times to use them. Tutorials for both the For Loop and While Loop structures are available if you are a beginner programmer or someone looking for more instruction on configuring these loops.

Contents

While Loops

While Loop is 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 terminal condition is evaluated.

Unlike a For Loop, While Loop execution does not depend on iteration count; thus, a While Loop executes indefinitely if the condition never occurs.

For more information on what a While Loop is, including its components and configuration options, look into While Loops in LabVIEW Help.

For Loops

For Loop is a structure you use to execute a block of code a set number of times. When the VI runs, the iteration count is evaluated, and then the code is executed.

A For Loop can be configured to conditionally stop code execution in addition to its iteration-based exit. In these cases, the code will execute until the count terminal setting is reached or the condition is met – whichever happens first.

Use Cases for Each Structure

While Loops should be used when:

  • You want your code to run indefinitely
  • You want to run code until a condition is met
  • You want user control over when code should stop
     

For Loops should be used when:

  • You need to run code for a certain number of times
  • You want to write a set number of measurements to a file
  • You want to take a certain number of data points
  • You want to run code until a condition is met OR a set number of iterations, whichever comes first