From Saturday, Nov 23rd 7:00 PM CST - Sunday, Nov 24th 7:45 AM CST, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW For Loops and While Loops Explained

Overview

This article explores some of the basic functions and uses of For Loops and While Loops in LabVIEW 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 in LabVIEW

While Loop is a structure you use to execute a block of LabVIEW 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. The While Loop will be a familiar concept for experienced programmers as it operates similarly in other computer languages.

LabVIEW While Loop flowchart

LabVIEW While Loop flowchart

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 in LabVIEW

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.

LabVIEW For Loop flowchart

LabVIEW For Loop flowchart

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

Was this information helpful?

Yes

No