0% found this document useful (0 votes)
6 views

LabVeiw For Loop and while loop

The document explains the For Loop and While Loop structures in LabVIEW, detailing their key features, components, and operational processes. The For Loop executes a set number of iterations determined by a count terminal, while the While Loop continues until a specified condition is met. Examples illustrate how to implement each loop for specific tasks, along with best practices and comparisons between the two types of loops.

Uploaded by

chavanakshay1812
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

LabVeiw For Loop and while loop

The document explains the For Loop and While Loop structures in LabVIEW, detailing their key features, components, and operational processes. The For Loop executes a set number of iterations determined by a count terminal, while the While Loop continues until a specified condition is met. Examples illustrate how to implement each loop for specific tasks, along with best practices and comparisons between the two types of loops.

Uploaded by

chavanakshay1812
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

LabVeiw For 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.

Key Features of the For Loop in LabVIEW

1. Fixed Iteration Count:


o The number of iterations the loop executes is determined before the loop
begins execution.
o This is controlled by the count terminal (N).

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).

Components of a For Loop

1. Loop Border:
o The rectangular structure that defines the For Loop. Code placed inside this
boundary will execute repeatedly.

2. Count Terminal (N):


o Located at the top-left corner of the loop.
o Specifies the total number of iterations the loop will run.
o You can wire a constant or a control to this terminal.

3. Iteration Terminal (i):


o Located at the bottom-left corner of the loop.
o Outputs the current iteration count, starting from 0.

4. Auto-Indexing Input and Output:


o Arrays wired into the loop automatically break into individual elements (if
auto-indexing is enabled).
o Outputs can collect data for each iteration into an array.
How It Works

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:

Add the first 10 natural numbers (1, 2, 3, ..., 10).

Steps in LabVIEW:

1. Place a For Loop on the block diagram.


2. Set the Count Terminal (N):
o Wire the constant 10 to the count terminal.
3. Add Numbers:
o Inside the loop, wire the iteration terminal (i) to an addition block.
o Add 1 to the iteration terminal value (because i starts from 0).
4. Sum the Numbers:
o Use a shift register to accumulate the sum.
5. Output the Result:
o Wire the final sum to an output tunnel.

Result:

The loop will iterate 10 times, adding 1, 2, 3, ..., 10, and output the result as 55.

Tips and Best Practices

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.

LabView While Loops


The While Loop in LabVIEW is a graphical programming structure that repeatedly executes
the code inside it until a specific condition is met. Unlike the For Loop, the While Loop does
not require a predetermined number of iterations and instead relies on a conditional (Boolean)
value to determine when to stop.

Key Features of the While Loop

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.

4. Execution at Least Once:


o The code inside the loop always executes at least once, even if the stop
condition is initially TRUE.

Components of a While Loop

1. Loop Border:
o The rectangular structure that defines the loop. Place code inside this boundary
to repeat it.

2. Iteration Terminal (i):


o Outputs the current iteration number.
o Useful for debugging or for use in logic within the loop.

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:

Acquire random numbers until their sum exceeds 50.

Steps in LabVIEW:

1. Place a While Loop on the block diagram.


2. Generate Random Numbers:
o Place a Random Number (0-1) function inside the loop.
3. Sum the Numbers:
o Use a Shift Register to accumulate the sum.
o Add the random number to the previous sum in each iteration.
4. Set Stop Condition:
o Compare the sum to 50 using a Greater Than function.
o Connect the result to the conditional terminal.
5. Output Results:
o Connect the sum to an output tunnel to display the final value.

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.

Comparison with For Loop

Feature While Loop For Loop

Execution Count Runs until a condition is met. Executes a fixed number of times.

Conditional Check Evaluated at the end of iteration. Not conditional, fixed by N.

Guarantee Always executes at least once. May not execute if N = 0.

You might also like