What Does a Loop Example Need?

Apr 13th, 2011 by Christina in Examples, LabVIEW

Loop Quandry Cartoon
The recent LabVIEW Example contest got me thinking about our example offerings. I categorize LabVIEW examples three ways:

  • Application examples. These examples are fully-functional applications that show off what LabVIEW can do. They can be great starting points for building your own, similar applications. However, they’re usually difficult to learn from because they are large and contain many different concepts. These kinds of examples seem to be popular in contests.
  • Concept examples. These are “teaching examples” that illustrate a concept. Although not VIs that you would modify and incorporate into a real application, they can present “how to” information with minimal extra code.
  • Function examples. I think these examples are the unsung heroes of the example world. With a good example, you can quickly learn what a function can do. Sometimes you can also learn what a function can’t do or common mistakes in its usage.

Even though Concept and Function examples are smaller than Application examples, they can be just as difficult to write. They require having a thorough understanding of the material without having lost sight of the new user’s point of view. In my experience, people who have taught LabVIEW are invaluable when it comes to crafting these kinds of examples.

Another recent, thought-provoking event for me was NI Tech, an NI-internal R&D conference. In two of her presentations, Nancy Hollenback (who recently re-joined NI as a Field Architect – more on that in a future post!) showed a slide illustrating several “hurdles” that LabVIEW users encounter when progressing from the “three icon demo” to large systems.

The first of these hurdles was “arrays and loops.”

Having been thus inspired, I’m now setting out to find (or build) the best possible function/concept examples for loops. There are somethings I already know they need to show:

  • how to decide if you should use a For loop or a While loop
  • array indexing on loop tunnels
  • the “zero iteration For loop” problem
  • early termination of For loops (“For loop with break”)
  • loop timing.

What other things would you expect from loop examples? Do you have any examples (from NI or elsewhere) that you recommend? Please send them my way!

9 Comments

  • Phillip Brooks

    Loop to loop signaling (not to be confused with data).

    NI touts the advantage and ease of parallel programming using multiple loops. A good, solid, reliable technique for signaling a consumer loop from a producer loop to exit would be useful for beginners.

    Current example? “Stopping Parallel While Loops without Reset.vi”. A friend was trying to learn how to do as the name suggested, but the example wouldn’t stop. I played around for a while and discovered that the label text had been expanded to two lines and if you try to toggle the switch using the TIP of the toggle switch, it won’t toggle because it is behind the label.

    Friend’s response, “Well THAT sucks. How would I know about that…”

  • Yikes, that’s awful! I’ll make sure that example is fixed. When you find things like this in the future, please post them to the NI forums so that our support staff can file a Corrective Action Request.

  • One valuable example: How to efficiently filter an array so that the output array contains only elements that meet constraint XYZ.

    The autoindexing example should discuss three issues:
    A) 2D arrays — row first or column first? What if you want the other one?
    B) You can index an input tunnel on a While Loop, but be aware that reaching the end of the array doesn’t stop the loop.
    C) What stops the loop if the N terminal is wired and there are multiple autoindexing tunnels? Yes, it’s documented, but an example would make it clear.

    Does “loop timing” include the Timed Loop? Does it include the parallel For Loop?

    The “zero iteration” problem means you’re going to have to discuss Shift Registers. Would your examples cover other aspects of shift registers, such as uninitialized shift register behaviors and growing the left-side shift register?

    How about coloring loops and labeling loops?

  • All good points! It seems like we should start with loop examples and then branch off to array manipulation examples.

    Yes, I think “loop timing” must include the Timed Loop, as well as acquisition-defined timing and the good-old-fashioned wait primitives.

    I consider the Parallel For Loop a completely separate topic, examples of which I would group with other ways to optimize performance (rather than with basic loop functionality).

    Shift registers probably should be part of the basic loop examples, yes. As for coloring and labeling loops, I don’t think we need an example as much as we need something like what’s been proposed on Idea Exchange.

  • Here are a couple more common use cases, although not really basic/fundamental:

    - calculating and displaying progress in a For Loop: percent complete as “i + 1 / N”

    - error handling in loops

  • I’d support your unsung heroes ;-)
    In my libs I have tests for almost all of the VIs that also act as an example how the VIUT was tested and was meant to be used. I believe NI would have such test VIs as well and could use those as examples. At least those would be a good ‘entry point’. BTW, as a German, I learned the hard way to use the original (english) LV & documentation, as the translation process – as good as it is – sometimes brings in another level of uncertainty or confusion. Interpreters are Humans only and as such may use a wrong or misguiding term…

  • Fabiola De la Cueva

    I like the example where the number wired at the N of the For Loop gets determined at run time. This is a good way to show how the For Loop does not execute if a 0 gets wired to the N terminal. A beginner would ask what is the use for that… well it lets me decide at run time how many times I want to run the loop or if I don’t want to execute the code at all.

    Another simple example is to show the difference between “continue if true” or “stop if true” in the While Loop.

  • I think you can show a simple example where you use all your cpu indefinitely at a while loop and your pc go very slow or crash, and show how to evite it (timing or events).

  • Event structure in while loop. :)
    Loop timing issue?