While Loop and For Loop Structures in Labview: Laboratory Activity No. 2
While Loop and For Loop Structures in Labview: Laboratory Activity No. 2
2
While Loop and For Loop Structures in LabVIEW
Loop and case programming structures control the execution flow in VI. In the
event of a certain condition arises during the execution of codes in the block
diagram, the execution flow may stop, repeat an action, or proceed to another
function.
While loop shown in Figure 1 repeats the codes inside its structure until the loop
condition terminal receives a specific Boolean value (true/logic 1 or false/logic 0).
By default the loop condition terminal is Stop-If-True; meaning, the while loop will
stop iterating until the loop condition terminal receives a true value. Another case
of loop condition is Continue if True which is opposite in operation to Stop-If-
True. It can be activated by right clicking on the loop condition terminal and
select Continue if True. One more terminal in while loop is called iteration
terminal that provides the current loop iteration count which is always started at
0. Every run of while loop the iteration count updates its value to the number of
iteration completed.
1|Page
For Loop Structure
A for loop executes the codes inside the structure repeatedly until the number of
iteration reaches the value specified at the count terminal. An empty for loop
structure is shown in Figure 2.
Loop
Count
Loop
iteration
Figure 2. For Loop Structure
2|Page
CREATING A VI with while loop
The following exercise demonstrates the use of while loop in a VI. This VI will
generate random numbers from 0 to 100. It will continuously generate a random
number until the VI detects the number of random number generated is less
than the number set by the user or a click on the stop button by the user.
3|Page
5. Create the block diagram as shown in Figure 5.
The following exercise demonstrates the use of for loop in a VI. This VI will
display the smallest number generated by the random number generator. The
number of random number generated is controlled by the value set in the loop
count.
5|Page
Selector
Shift
Register
➢
For loop
Functions Palette → Programming → Structure → For Loop
➢
Selector
Function Palette → Programming → Comparison → Select
➢
Shift Register – is a part of loop structure that allows you to store values
created from the codes inside the loop and pass this values from one
iteration to the next iteration. To create a shift register, right click on the
right border of the for-loop structure and select add shift register.
6. Run the VI with the different values of number of iterations and verify
the results.
6|Page
Exercises:
1) Create a VI with while loop that generates random number from 0 – 100
until the random number is greater than 25 but less than 75.
2) Create a VI with for loop that generates random number from 0 – 100
and counts the number of random number generated of less than 50.
3) Create a VI with for loop that will get the sum of arithmetic progression.
The user will input the starting value, the common difference, and the number
of terms in the arithmetic progression.
7|Page
SUMMARY OF LEARNING OUTCOMES:
Our first impressions from the doing the two
schematics is that the “for loop” has a designated stop
point up until the conditions are met. While in contrast the
“while loop” is the one that whatever or how long will it
take up until all conditions are met it will continue on and
on.
8|Page
9|Page
DISCUSSION:
10 | P a g e
CONCLUSION
11 | P a g e
REFERENCES:
https://techdifferences.com/differenece-between-for-and-while-loop.html#:~:text=In
%20while%20loop%20if%20initialization,each%20time%20the%20loop
%20iterate.&text=In%20'for'%20loop%20iteration%20statement,written%20anywhere
%20in%20the%20loop.
https://www.quora.com/What-is-the-difference-between-while-loop-and-for-loop-What-
is-an-example-of-this
12 | P a g e