Configuring LabVIEW For Loops to Exit Conditionally

Publish Date: Jan 10, 2011 | 14 Ratings | 3.43 out of 5 |  PDF

Overview

Since its inception in 1986, National Instruments LabVIEW software has incorporated programming concepts similar to those of text-based languages. For example, programming statements such as For Loops and While Loops allow code to be executed repeatedly. Text-based languages also provide methods for terminating loop execution upon a specified condition; in LabVIEW, only the While Loop allowed this behavior prior to NI LabVIEW 8.5.

Table of Contents

  1. Adding a Conditional Terminal to a LabVIEW For Loop
  2. Background Information
  3. Additional Notes on For Loops with Conditional Exits
  4. Learn More About LabVIEW
  5. Related Links

1. Adding a Conditional Terminal to a LabVIEW For Loop

With LabVIEW 8.5, you can add a conditional terminal to configure a For Loop to stop when a Boolean condition or an error occurs. A For Loop with a conditional terminal executes until the condition occurs or until all iterations are complete, whichever happens first. For Loops you configure for a conditional exit have a red glyph next to the count terminal (N ) as well as a conditional terminal in the lower right corner. Once you configure the For Loop to exit conditionally (in other words, on a break), the loop appears similar to Figure 1.

Figure 1. For Loop Configured for a Conditional Exit

To add a conditional terminal to a For Loop, right-click on the For Loop border and select Conditional Terminal from the shortcut menu. You must then wire the conditional terminal and either wire the count terminal or auto-index an input array for the loop to execute and for the VI to run.

Figure 2. Adding a Conditional Terminal to a For Loop

Back to Top

2. Background Information

Programmers often need to stop a loop when a specific condition occurs. Text-based languages include statements that allow both For Loops and While Loops to exit upon a specified condition. For example, in the following sample C code, a the loop is programmed to iterate 50 times (from 0 to 49). However, the loop stops execution using the Break function after 11 iterations, when i = 10.

int i;
i = 0;
for (i = 0; i < 50; i++)
   {
   if (i == 10)
      break;
   }

A LabVIEW programmer who needs to iterate over all the elements in an array often uses a For Loop because it allows the auto-indexing of the array. Typical LabVIEW code looks like Figure 3.

Figure 3. LabVIEW Code for a For Loop

While this solution is functionally correct, the For Loop visits every element in the array, even if the exit condition occurs in an early iteration of the loop. For greater efficiency, you can instead use a While Loop (see Figure 4). This solution, which is more complex and requires more code, iterates over the array until the exit condition occurs or the end of the array is reached. As shown in Figure 1, a For Loop configured for a break condition is much simpler and more elegant solution.

Figure 4. A While Loop Configured To Index an Array

You also can use a While Loop to search an array; when you have found your item of interest, the loop stops executing. If the item is not in the array, the result is an infinite loop.

  

Figure 5. While Loop Potentially Leading to an Infinite Loop

A better solution is to use a For Loop configured for a break. If the item of interest is not found in the array, the loop stops after N iterations.

 Figure 6. For Loops Mitigate the Chance of an Infinite Loop

Back to Top

3. Additional Notes on For Loops with Conditional Exits

A For Loop configured for a conditional exit might not execute N times (or over an entire auto-indexed array). Once configured to exit conditionally, N represents the maximum number of possible loop iterations, but it is not necessarily the exact number of times the loop will run.

The conditional terminal behaves the same as it does in a While Loop. You can switch the conditional terminal to Continue if True or wire an error cluster to it (see Figure 7). You cannot delete the conditional terminal; to remove the conditional terminal, you must deselect the Conditional Terminal option in the right-click menu of the loop.

 

Figure 7. Wiring an Error Cluster to the Conditional Terminal

If you configure a For Loop to exit conditionally, you cannot predict the size of any auto-indexed (accumulated) output arrays, although you might know the upper bound. The number of elements in an auto-indexed array output equals the number of times the loop iterates.

Back to Top

4. Learn More About LabVIEW

To learn more about features and changes in LabVIEW, visit What's New in LabVIEW.

Launch LabVIEW online to use the latest features.

Back to Top

5. Related Links

LabVIEW Help: For Loop and While Loop Structures
LabVIEW Help: Adding a Conditional Terminal to a For Loop
Manuals: LabVIEW 8.5 Upgrade Notes

  

Back to Top

Bookmark & Share

Ratings

Rate this document

Answered Your Question?
Yes No

Submit