LabVIEW 8.5 Feature – For Loop Conditional Terminal

Posted on Monday 7 July 2008

One of the great new features in LabVIEW 8.5 is the addition of an optional Conditional Terminal on the For Loop structure.  This allows you to easily "break" out of the For Loop, when an exit condition occurs. (In fact, text based programming languages often let you use the "break" command to exit a For Loop.)

In the screenshot below, you can see the difference between the classic For Loop and one with a Conditional Terminal.



Show the Conditional Terminal by right-clicking on the frame of the For Loop structure and selecting Conditional Terminal from the context menu.



When would you want to use the Conditional Terminal?

For Loop are commonly used to iterate over an array of items, either operating on the items or checking for some condition within the items is satisfied (for example searching for an element with a certain value).

Sometimes, you might want to stop iterating over the items (i.e., break out of the For Loop) if:
  • your objective was satisfied -- e.g., you found the element (s) you were looking for), or
  • an error occurred during an iteration and it doesn't make sense to iterate over the remaining items.

Example 1 - Doing work in a For Loop that can generate errors

In LabVIEW versions <= 8.2, you might have implemented some code that looks like this:



This example, takes an array of file paths and deletes any files that are larger than some maximum file size. (Warning: you might want to be careful with such a VI, since it deletes files.)

You can see that, in the event an error occurs, the VI will not do any more work.  It will just finish iterating over the array, passing the error through, before it exists.



Now, in LabVIEW 8.5, we might want to rewrite this example, by wiring the error cluster to the Conditional Terminal of the For Loop in order to stop it, in the event of an error.



The above code that uses the Conditional Terminal will finish sooner, in the event of an error, and is also much more readable.

Example 2 - Searching an array in a For Loop

Now, let's look at different example where we want to search an array for an element.  Here is what our code might look like in LabVIEW <= 8.2.



And, here's how we could improve the performance in LabVIEW 8.5, using the Conditional Terminal.



By using a Conditional Terminal to stop the search once we've found the element we're looking for, we can exit the loop early and save CPU cycles.

What do you think about this new feature?

These are just a couple simple examples of how using the Conditional Terminal of the For Loop in LabVIEW 8.5 can improve the readability and performance of your code.

What other patterns of use have you identified?  Are there any benefits or drawbacks that you can think of?  I'm interested to know whether you've tried using this new feature and if you've had success with it, in improving your code.

25 Comments for 'LabVIEW 8.5 Feature – For Loop Conditional Terminal'

  1.  
    Ben
    July 7, 2008 | 8:27 am
     

    I use whenever I’m working in LV 8.5.

    I have forced myself to make sure I always put the condition terminal in the bottom right-hand corner evn though it looks cooler witha nice short wire.

    For other readers, remeber to handle zero-iteration conditions.

    Ben

  2.  
    Omar Mussa
    July 7, 2008 | 8:54 am
     

    I think the best part about using a Conditional Terminal is not well shown… the combination of Example 1+2 which nets the ability to pass out the ‘error’ VI path. If you add an output to the For Loop (in Example 1 it would be the File Path) with autoindexing disabled (as in Example 2) you will get the last path that was executed — this is really helpful if you’re doing debugging and want to see which value is causing the error (for example).

  3.  
    July 7, 2008 | 9:02 am
     

    Ben: Yes, we can’t forget that this is still a For Loop and that we must remember that zero-iteration conditions can occur. I believe that my examples handle this gracefully (either by use of shift registers or default values of Boolean output tunnels being the same as “not found”). Excellent point!

    Omar: Yes, you’re right. One of the nice things about “breaking” (stopping) a For Loop is that you have easy access to all the last iteration values (the ones that caused the early exit) at the output tunnels of the For Loop.

    Maybe these two point you guys have raised would make for a nice follow-up article ;)

  4.  
    Neville D
    July 7, 2008 | 10:13 am
     

    Great article! I use FOR loops with a break all the time.

    One pt: there is a slight improvement in performance when using a FOR loop wrt a while loop, and sometimes every bit of speed improvement helps.

  5.  
    crelf
    July 7, 2008 | 3:10 pm
     

    Neville D wrote: “…there is a slight improvement in performance when using a FOR loop wrt a while loop…”

    Is there a reference for this? LAVA thread or NI Discussion Forum thread maybe?

  6.  
    Bob Hamburger
    July 7, 2008 | 8:18 pm
     

    There’s nothing that this new construct offers that couldn’t be done previously with a well-thought out While Loop. Personally, in situations which might warrant using the For Loop conditional terminal, I prefer just using a While Loop programmed with the appropriate termination conditions. It forces me to consider exactly how the loop will function and under what conditions it will finish. Also, I’ve always believed that there is a clear distinction of when to use a For vs. While Loop. If the loop will have a known, a priori number of iterations, you use a For Loop. If the number of iterations will vary as a function of what happens in the loop, then you use a While Loop. Programming this way allows this crucial loop behavior to be discerned even at a casual glance. In my mind, the hybrid construct encourages sloppy thinking and bad programming. But that’s just my opinion.

  7.  
    July 7, 2008 | 10:46 pm
     

    Bob: I agree that there’s nothing new that a Conditional Terminal in a For Loop enables. I guess it’s a matter of context — when I’m iterating over an fixed-length, ordered set of items I just want to do it in a For Loop. For me, doing it in a For Loop is a form of documentation (it’s like stating, “for each element in set”). The early-exit condition is just an exception to my intent. However, with a While Loop, the intent is to exit when some exit condition occurs that could be just about anything and completely unrelated to iterating over my set of items. The For Loop was designed for iterating over sets — but really, we don’t even need For Loops, as we can just use While Loops. If we’re going to stop using the Conditional Terminal in For Loops, we might as well stop using For Loops, too. That’s my “devil’s advocate” response to your argument ;) Cheers – Jim

  8.  
    Ben
    July 8, 2008 | 5:44 am
     

    crelf wrote “Is there a reference for this? LAVA thread or NI Discussion Forum thread maybe? “

    Check either the LV 6.0 or 6.1 release notes about how For Loops are optimized vs While loops.

    The addition of the condition terminal did not make the For loop any slower. In fact, its now faster since it can now complete earlier.

    I’d bet a beer that it would be hard to develop two functionally identical examples (one with for one with While) where the While would fair better.

    Ben

  9.  
    crelf
    July 8, 2008 | 7:34 am
     

    Bob Hamburger wrote: “There’s nothing that this new construct offers that couldn’t be done previously with a well-thought out While Loop.”

    There’s bound to be resistance to every new feature of LabVIEW. I agree with the statement above, but I don’t think there’s any real reason to shun new funcitonality just because it can be done in a different way that forces you to think in a certain way. I think that most relatviely experienced programmers understand the power (and responsibility) of the conditional terminal foor loop. I agree that it could be abused by people new to programming, but that can be applied to just about everything in LabVIEW :)

  10.  
    crelf
    July 8, 2008 | 7:39 am
     

    Ben said: “Check either the LV 6.0 or 6.1 release notes about how For Loops are optimized vs While loops.”

    Thanks Ben.

    Ben said: “The addition of the condition terminal did not make the For loop any slower. In fact, its now faster since it can now complete earlier.”

    :D That doesn’t make the loop iteration faster – the statement conditional forking is completely different, so I don’t think that’s a valid comparison. Is this what you actually meant? Or is it that a 100 iteration for loop runs faster than a 100 iteration while loop (with the same code in them, of course).

    I’d bet a beer that it would be hard to develop two functionally identical examples (one with for one with While) where the While would fair better.

  11.  
    crelf
    July 8, 2008 | 7:40 am
     

    Ben said: “I’d bet a beer that it would be hard to develop two functionally identical examples (one with for one with While) where the While would fair better.”

    I think you’re probably right :D I agree enough not to bet beer on it…

  12.  
    July 8, 2008 | 10:14 am
     

    The disagreement over While Loop and For Loop constructions come down to differing perspectives. Those favoring the While Loops are looking at the exit condition (viz specified dynamically within execution of the code), whereas those favoring For Loops are looking at the input data (viz, operating on an incoming, known array of “something”). This is clear in the notes above. While Loop proponents BELIEVE it’s sloppy to NOT focus on exit condition being discerned dynamically (which is the essence of a While Loop), whereas For Loop proponents say that operating on a known input array, it is useful to have a dynamically specifiable exit condition IN ADDITION. That way BOTH of those aspects of the code already documented in place by the code itself.

    Personally I favor the For Loop with Conditional Exit/Termination but this choice really reflects more an overall programming paradigm issue.

    FWIW, I remember notes indicating that For Loops do execute faster than While Loops in v6x, if not even 7x. I don’t know if that’s still the case, however, in 8.5.

  13.  
    Yair
    July 8, 2008 | 11:22 am
     

    I’m fairly certain I remember the guy who created this writing that he was surprised to see how much faster it was compared to the while loop. If memory serves, the memory allocation was the main reason – the FLwB allocates all the memory for indexed outputs in a single call and then truncates it if necessary. The while loop has to resize the buffers as it goes.

    I couldn’t find the specific thread at the moment, which might mean that it was another developer at NI, or it might have been in the beta board for the version where this feature was introduced.

  14.  
    crelf
    July 8, 2008 | 12:23 pm
     

    Yair wrote: “memory serves, the memory allocation was the main reason – the FLwB allocates all the memory for indexed outputs in a single call and then truncates it if necessary. The while loop has to resize the buffers as it goes.”

    That sounds like it’s plausable, but you’re right: only if you’re indexing an output. What I’m trying to determine is if there are timing differences between the for and while loops inherently, which is what it sounds like Neville D is trying to say.

  15.  
    July 8, 2008 | 7:42 pm
     

    Valdeane: Thanks for taking the time to summarize both the While Loop and For Loop preference perspectives. Yes, it comes down to how you look at the problem and then map the tools into a solution.

  16.  
    Yair
    July 10, 2008 | 11:34 am
     

    OK, Toby found a quote from Robbie, although it wasn’t the one I remembered specifically:

    http://eyesonvis.blogspot.com/2007/08/for-loop-with-conditional-terminal.html

  17.  
    Polarcoordinate
    August 13, 2008 | 4:18 am
     

    Huh? So much excitement about the ability of breaking out of a for-loop? Most, if not all “legacy, text-based” languages have had that feature for ever…

  18.  
    crelf
    August 13, 2008 | 8:19 am
     

    Do you feel better Polarcoordinate, now that you’ve gotten that elitism off your chest?

  19.  
    Polarcoordinate
    August 14, 2008 | 5:49 am
     

    crelf: I’m not trying to be “elitarian”. I just think there’s so much marketing around LabView claiming that it is so much better than “text-based”-languages (same with Matlab). When you come to LabView from a “text-based” background and find a lot of features that you are used to missing (like the one just added), it’s really frustrating to see that when they are added, they are presented as “great new features”.

    By the way, coming back to this page after a few days, I just put example 2 into my VIs :-)

    PS: I don’t just complain, I have already made (hopefully useful) suggestions to NI via the contact-forms.

  20.  
    Polarcoordinate
    August 14, 2008 | 6:22 am
     

    Would it be possible to process the elements of the array in reverse order? If I wanted to find the last match in example 2 instead of the first match? Otherwise I’d still have to revert to the while-loop to and manually decrement the index…

  21.  
    August 14, 2008 | 9:08 am
     

    Polarcoordinate: The easiest way to iterate over an array in reverse order is to reverse the array (using Reverse 1D Array) before you pass it into the For Loop.

  22.  
    crelf
    August 14, 2008 | 9:25 am
     

    Polarcoordinate: Just because it’s common-place somewhere else doesn’t mean that we can’t get excited about it becomming available to us. They *are* “great new features” for us, and that’s why we’re excited about them. Rather than saying “it’s great that we got this feature that’s available in other languages”, we’re simply saying “it’s great that we got this feature”. It’s not a competition between different languages, but more between successive versions of LabVIEW – it’s about celebrating the improvement of LabVIEW (I couldn’t care less about what’s available in other languages). Sure, other languages have heaps of features that LabVIEW doesn’t, but LabVIEW has heaps of features that other programming languages don’t. I don’t see how comparing a feature release in LabVIEW to features available in other languages is of any value to LabVIEW programmers, other than to put LabVIEW down. Personally, I use it because I like programming with it and it is a great tool to help me do what I need to get done.

    I don’t think that continuing this thread on Jim’s blog adds any value either, so that’s all I’ll say about it here. You’re welcome to continue discussing it further, but if you want any more of my input, I suggest you move the thread over to LAVA or the like.

  23.  
    Ton Plomp
    August 14, 2008 | 11:28 am
     

    About feature request: When you come to LabView from a “text-based” background and find a lot of features that you are used to missing (like the one just added),

    PS: I don’t just complain, I have already made (hopefully useful) suggestions to NI via the contact-forms.

    Good you suggested them to NI. Would you be willing to start a thread on the LAVA forums under ‘Feature requests‘? so we can chime in? I have to text-based background so I don’t know what I’m missing but am eager to learn.

    Ton

  24.  
    Pat Lavezza
    August 15, 2008 | 12:08 am
     

    Ok, I know this is nitpicking, but in example 2 the code isn’t equivalent, is it? Wouldn’t the 8.2 code find the index of the last element that matched while the 8.5 code would find the first element. I think there is an assumption there that the search value is only in the array once. Of course, I realize it’s just an example and we’d use Search 1D Array with and optional Reverse 1D Array to find the first and last matches in real life.

  25.  
    August 15, 2008 | 8:21 am
     

    Pat: Yes, you’re right. That’s a bug :)

Leave a comment

(required)

(required)


Information for comment users
Line and paragraph breaks are implemented automatically. Your e-mail address is never displayed. Please consider what you're posting.

Use the buttons below to customise your comment.


RSS feed for comments on this post |

 

Bad Behavior has blocked 741 access attempts in the last 7 days.