LabVeiw For Loop and while loop
LabVeiw For Loop and while loop
The For Loop in LabVIEW is a structure used to execute a set of code multiple times, a
specific number of times. It is similar to the "for" loop found in other programming languages
but is implemented graphically in LabVIEW.
2. Automatic Indexing:
o If an array is wired to the loop, the loop automatically iterates over the
elements of the array.
o Each iteration processes one element of the array.
3. Iterative Value:
o The iteration terminal (i) provides the current iteration index, starting from 0.
4. Data Tunnels:
o Data can enter and leave the loop via tunnels. Tunnels appear as small squares
on the boundary of the loop.
o When exiting, you can choose whether the loop outputs a single value or an
array of values (auto-indexing).
1. Loop Border:
o The rectangular structure that defines the For Loop. Code placed inside this
boundary will execute repeatedly.
1. Initialization:
o When the loop starts, the count terminal (N) is evaluated to determine the total
number of iterations.
2. Execution:
o The code inside the loop executes repeatedly for each iteration, incrementing
the iteration terminal (i) after each run.
3. Termination:
o When the loop completes all iterations (or an optional stop condition is met in
advanced cases), it stops, and output data is passed out through the tunnels.
Example
Problem:
Steps in LabVIEW:
Result:
The loop will iterate 10 times, adding 1, 2, 3, ..., 10, and output the result as 55.
1. Array Handling:
o Enable auto-indexing for array inputs/outputs for element-wise operations.
o Ensure all arrays have compatible sizes to avoid runtime errors.
2. Performance:
o Use shift registers for values that need to be carried over iterations.
o Avoid wiring excessively large arrays directly into the loop.
3. Visualization:
o Use Probes to monitor the values of wires during execution for debugging.
4. Termination:
o Although For Loops run for a fixed count, combining it with While Loops for
conditional termination is possible for advanced use cases.
1. Conditional Execution:
o The loop runs until the specified stop condition evaluates to TRUE.
o It evaluates the condition at the end of each iteration (post-test loop).
2. Data Tunnels:
o Data enters and exits the loop through tunnels.
o Tunnels can pass a single value or accumulate values into arrays using auto-
indexing.
3. Iteration Terminal:
o Tracks the number of iterations completed by the loop, starting from 0.
1. Loop Border:
o The rectangular structure that defines the loop. Place code inside this boundary
to repeat it.
3. Conditional Terminal:
o Located at the bottom-right corner of the loop.
o Requires a Boolean input to determine whether the loop should continue.
o By default, the loop stops when this terminal receives a TRUE signal (can be
inverted).
4. Tunnels:
o Allow data to pass in and out of the loop.
o Auto-indexing can be enabled for arrays, allowing the loop to process
individual elements.
How It Works
1. Initialization:
o The loop starts and initializes any inputs or controls connected to it.
2. Execution:
o Executes the code inside the loop.
o At the end of each iteration, the stop condition is evaluated.
3. Termination:
o The loop stops when the stop condition evaluates to TRUE.
Example
Problem:
Steps in LabVIEW:
Result:
The loop will run until the sum of random numbers exceeds 50 and then stop.
Advanced Features
1. Shift Registers:
o Used to pass data from one iteration to the next.
o Essential for operations like running sums or counters.
2. Auto-Indexing:
o Process arrays element by element within the loop.
o Enable auto-indexing on input or output tunnels to work with arrays.
3. Timing Control:
o Use the Wait (ms) function to control the execution speed of the loop.
o This is important for loops running continuously to avoid CPU overload.
Execution Count Runs until a condition is met. Executes a fixed number of times.