[go: up one dir, main page]

0% found this document useful (0 votes)
163 views12 pages

While Loop and For Loop Structures in Labview: Laboratory Activity No. 2

The document discusses while and for loops in LabVIEW. It provides examples of using while and for loops to generate random numbers. A while loop will continuously execute code until a condition is met, while a for loop will execute a set number of times as determined by its loop counter. Exercises are provided to create VIs using both while and for loops to generate and analyze random numbers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
163 views12 pages

While Loop and For Loop Structures in Labview: Laboratory Activity No. 2

The document discusses while and for loops in LabVIEW. It provides examples of using while and for loops to generate random numbers. A while loop will continuously execute code until a condition is met, while a for loop will execute a set number of times as determined by its loop counter. Exercises are provided to create VIs using both while and for loops to generate and analyze random numbers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 12

LABORATORY ACTIVITY NO.

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 Structure

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.

Figure 1 While Loop Structure

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.

1. Open the labVIEW.


2. Create new VI.
3. Save the VI as Random Numbers.vi.
4. Create the front panel as shown in Figure 4.

Figure 4 Random Numbers.vi Front Panel



Random Number Generated
Control palette → Numeric → Numeric Indicator

Number Set by the User
Control palette → Numeric → Numeric Control

Stop
Control Palette → Boolean → Stop Button

3|Page
5. Create the block diagram as shown in Figure 5.

Figure 5 Random Numbers.vi Block Diagram



While loop
Function Palette → Programming → Structure → While Loop

Wait Until Nest Milliseconds Multiple
Functions Palette → Programming → Timing → Wait Until Nest ms
Multiple
➢ Random Number Generator
Functions Palette → Programming →Numeric → Random Number (0-1)
➢ Round to Nearest
Functions Palette → Programming →Numeric → Round to Nearest
➢ Less? (Is X less than y?)
Function Palette → Programming → Comparison → Less?
➢ Or (X OR Y)
➢ Function Palette → Programming → Boolean → Or
4|Page
6. Run the VI and confirm its operation. While at the block diagram window,
run the VI again with the Highlight Execution button is activated. Observe the
data flow operations.

Creating a VI with For Loop

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.

1. Open the labVIEW.


2. Create new VI.
3. Save the VI as Random Numbers.vi.
4. Create the front panel as shown in Figure 6.

Figure 6. Smallest Number.vi Front Panel

5. Create the block diagram as shown in Figure 7.

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.

Name: __Josef Geminiano__ Instructor’s Initial: ______________


Student Number: _20151144193__ Date performed: _9/4/20_

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:

In the whole experiment we encounter both the “For


loop” and “while loop”. For loops, are great for when you
already know how many times you want to loop through
something. When using a for loop, we typically use a
counter that will either increment or decrement until a
condition is met. Once the condition is met, the loop will
stop. while in contrast A while loop is slightly different
than a for loop for the fact that it’s good to use when we
don’t know how many times, we want to loop through a
problem beforehand. This is the key difference between
using a for loop or a while loop. over all these two "loops"
are used to have a necessary condition to be met in various
use cases.

10 | P a g e
CONCLUSION

Loops do exactly what they sound like, they allow us


to run the same code over and over again until a stopping
condition is met. This can be extremely useful for all kinds
of things in coding and is one of the fundamental building
blocks of web development. so in conclusion when this
kind of codes are very useful in order to aggregate what is
the necessary output by the use of repetition.

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

You might also like