For Loop Examples
Thanks for all the responses to my last post! The humble For Loop is much more complicated than it appears at first glance. (I’ll get to While Loops as well, but later).
Here’s a recap of the suggested “function” examples:
- Contrasting the For Loop’s operation (iteration from zero to N times) with a While Loop’s operation (iteration until a condition is met).
- Automatic indexing
- 1D array
- 2D array (row vs. column, how to transpose if needed)
- Multiple indexing sources (and which will stop the loop)
- Shift registers
- passing data to the next iteration of the loop
- uninitialized shift registers
- shift registers with depth > 1
- Reading the N terminal inside the loop
- Conditional terminal to “break” the loop early
And the suggested “concept” examples:
- How to stop the execution of parallel loops
- How to control loop timing (including the Timed Loop)
- For Loops that execute zero times – how to know when it can happen and how to handle it correctly
- Error handling in loops
- Data communication between loops. (I’m going to punt on this one. Anyone who heard about this year’s CLA Summit knows that Data Communication in LabVIEW is a huge topic, and not something I can address with a simple example. Rest assured, there are people working on clarifying the data communication options for our customers and I’ll keep you posted about what they come up with).
- Performance considerations (e.g. optimizing loops), including the Parallel For Loop.
And the suggested “application” examples:
- Filtering an array to only the elements meeting a constraint
- Displaying progress of a loop
Whew! Well, as the saying goes, the longest journey begins with a single step, so let’s get started.
Here’s a simple For Loop example: For Loop Basics (I’m not claiming this VI is perfect; it’s just something I put together quickly).


Here are some experiments I’d like feedback about:
- It’s an exercise. Rather than just giving you something to run, it has steps which require you to edit the VI. Do you like examples like this?
- The instructions are on the panel in a String control. This approach has some advantages over a free label (e.g. it gives us a scrollbar, which handles system font changes more gracefully) and some disadvantages (e.g. you probably wouldn’t use controls for documenting “real” VIs). What do you think of this idea? Also, would you rather have the instructions on the diagram?
- It’s built so that it can be a VI Snippet, which means that (if you’re using a supported web browser and LabVIEW 2009 or later) you can drag it to a VI diagram. Is this useful at all?

Hi Christina,
Thanks for continuing to put so much attention into this!
Here’s some off-the-cuff feedback to each of your requests:
(1) I think it depends on the context, and I’d differentiate between an example and a *tutorial*. The exercise you’ve written is definitely more of a tutorial (as opposed to “here’s some sample code that acquires and filters data”). In the context of someone learning the basics of the language I think a tutorial (like what you’re doing) is totally appropriate *if* people understand what they’re looking for. For instance, if my “Find Examples…” dialog in LabVIEW starts filling up with tutorials rather than example code, that might bug me a little.
(2) I think your advantages are spot-on. That said, this is not a coding/documentation practice I’d ever recommend for project work: it results in “dead” indicators on the block diagram, you have to get the default value of the indicator right, etc. In the context of teaching newbies about LabVIEW I feel like I’d err on showing them actual good documentation practices rather than something I’ll have to train them out of later. Maybe the correct approach is to file a bug on how LabVIEW handles system font changes in free labels
.
(3) If this was part of a web-based series of tutorials (i.e. something totally outside of LabVIEW) I think the snippet is a great idea! “To begin each exercise, just drag the snippet image to a new block diagram in LabVIEW.” — I think that’s a sweet way to bridge between where the student is *reading* the exercise and where they’re *doing* the exercise. That’s especially true when the alternative is what? — “Click to download this sample VI, then find it on disk, then double-click it to open it in LabVIEW.” Bleh.
Thanks for the feedback, Justin! I’ll continue revising my ideas for examples and tutorials.