Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 36 ยท Dataflow and Structures

While Loops and For Loops

Use LabVIEW While Loops and For Loops correctly: iteration terminal, stop conditions, tunnels, auto-indexing, conditional terminals and loop timing.

Almost every real LabVIEW program contains a loop. Acquisition loops read hardware, user-interface loops wait for clicks, and processing loops walk through arrays. This lesson covers the two loop structures, how data enters and leaves them through tunnels, and how to time and stop them safely. LabVIEW cannot run here, so no real output appears; JavaScript stand-ins model the behavior.

The While Loop

Find it in Programming > Structures. Drag a rectangle around the code you want repeated. A While Loop has two special terminals:

  • The iteration terminal i (blue box in the corner) gives the current iteration number, starting at 0.
  • The conditional terminal (red circle) decides when to stop.

The loop runs its body at least once, then checks the conditional terminal. By default the terminal is Stop if True: when it receives True, the loop ends. Right-click it to change to Continue if True. Wire a Stop button (a Boolean control) to it for a user-controlled loop.

Always run the body first and test afterward: a While Loop is a do-while, never a while.

The For Loop

A For Loop runs a fixed number of times, set by the count terminal N in the top left. The iteration terminal i also starts at 0 and ends at N - 1. If you wire an array into the loop with auto-indexing on, N is set automatically to the array length.

A For Loop can also have an optional conditional terminal (right-click the border > Conditional Terminal), giving a for with early exit. If N is 0 the body does not run at all, which makes it safe for empty arrays.

While versus For

While Runs until a condition
For Runs N times
While runs once at least
For may run zero times
1 / 2

Tunnels and auto-indexing

Data crosses the loop border through tunnels. A wire into the loop becomes an input tunnel and a wire out becomes an output tunnel.

  • Auto-indexing enabled (the default for arrays on For Loops): the tunnel breaks an array into elements on the way in, one per iteration, and builds an array on the way out.
  • Auto-indexing disabled: the whole array passes through as one value. For output, only the last iteration's value emerges.

Right-click a tunnel and choose Enable/Disable Indexing. The tunnel's look tells you: a plain square means pass-through; a square with brackets means indexing. On a While Loop auto-indexing is disabled by default, which surprises beginners. Turn it on when you want a result array.

Timing the loop

A loop with nothing in it spins at full CPU speed. Almost always you want to pace it:

  • Wait (ms) waits at least the given milliseconds. Simple, but the total period is the wait plus the body time.
  • Wait Until Next ms Multiple aligns to a clock grid, so the period stays near constant even if the body time varies. Use it for regular sampling.
  • Hardware-timed loops (see the DAQ chapter) take their pace from the device.

Without a wait, a user-interface loop can use 100 percent of a CPU core. A wait of 10 to 50 ms feels instant to a human and costs almost nothing.

Stopping loops safely

  • Wire a Stop button to the conditional terminal. Use a Latch When Released mechanical action so the button resets automatically.
  • Do not rely on Abort.
  • If several parallel loops must stop together, they need a shared signal. Options include a stop local variable (read-only in the loops, written once), a notifier, or a queue. A local used purely as a "stop" flag written by one source is a widely accepted exception to the "avoid locals" rule.
  • After a loop ends, close files, sessions and hardware tasks in code that runs afterward.

Other loop helpers

  • Shift registers carry values from one iteration to the next. See shift registers and feedback.
  • Stop on error: wire an error cluster into Unbundle by Name and OR the status with the Stop button.
  • Concurrent iterations: right-click a For Loop and enable Allow Parallel Loop Iterations to spread independent iterations across processors. Only do this when each iteration does not depend on the previous.

Stand-in: While loop as do-while

This models a While Loop with an iteration counter and a stop condition. The body always runs once. The result array shows the difference between indexed output (all values) and non-indexed (last value).

JS Stand-in: While Loop, tunnels and indexing

Press Run (or Ctrl + Enter) to see the console output here.

No output.

A second stand-in compares a For Loop over an array with auto-indexing on the input and output, plus an empty array.

JS Stand-in: For Loop with array input

Press Run (or Ctrl + Enter) to see the console output here.

No output.

An empty While Loop with a Stop button and no wait can hog a core and make the whole computer feel slow. Always add a Wait.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the first value of the iteration terminal i?

    Loop iteration counts start at 0 in both loop types.
  2. 2. How many times does a While Loop run its body at minimum?

    The conditional terminal is checked after the body, so it runs at least once.
  3. 3. What is the default auto-indexing state on a While Loop tunnel?

    While Loop tunnels default to non-indexing; For Loops default to indexing for arrays.
  4. 4. Which function keeps a regular sampling period best?

    Wait Until Next ms Multiple aligns to a clock grid, regardless of body time.

Summary

  • A While Loop runs at least once and stops when its conditional terminal fires; a For Loop runs N times and may run zero times.
  • The iteration terminal starts at 0.
  • Tunnels with auto-indexing turn arrays into elements and back.
  • Always pace loops with a wait, and stop them with a button, never Abort.
  • Parallel loops need a shared stop signal such as a notifier or queue.

Next: Shift Registers and Feedback