Summary: To pass data out of a While Loop through a tunnel.
Note: Your browser may not currently support MathML. See our browser support page for additional details. You can always view the correct math in the PDF version.
Complete the following steps to build a VI that generates random
numbers until the number generated matches a number you
specify. The iteration terminal records the number
of random numbers generated until a match occurs.
![]() |
Controls>>Numeric Controls palette, on the
front panel. Label the control Number to
Match. This control specifies the number to
match.
Controls>>Numeric Indicators palette, on
the front panel. Label the indicator Current
Number. This indicator displays the current
random number.
Number of
iterations. This indicator displays the number of
iterations before a match.
Set a data range for a control to prevent the user from
selecting a value that is not compatible with a range or
increment. You can choose to ignore a value that is out of
range or coerce it to within the range. Complete the following
steps to set the range between
0 and 10000 with an increment of
1 and a default value of 50.
Number to Match control and
select Data Range from the shortcut menu. The
Data Range page of the Numeric
Properties dialog box appears.
Use Default
Range checkbox.
Default Value to 50.
Minimum value to 0 and
select Coerce from the Out of Range
Action pull-down menu.
Maximum value to 10000
and select Coerce from the Out of
Range Action pull-down menu.
Increment value to 1
and select Coerce to Nearest from the
Out of Range Action pull-down menu. Do
not close the dialog box.
By default, LabVIEW automatically formats numeric
controls. You also can specify the precision or notation. You
can display numeric values in floating-point, scientific, or
SI notation. Complete the following steps to change the
precision to 0.
Format and Precision tab. If you
closed the Numeric Properties dialog box,
right-click the Current Number indicator and
select Format & Precision from the shortcut
menu. The Format & Precision page of the
Numeric Properties dialog box appears.
Floating Point and change
Significant digits to Digits of
precision.
0 in the Digits of
precision text box and click the OK
button.
Current Number and Number of
iterations indicators.
![]() |
Place the Random Number (0-1) function,
located on the Functions>>Arithmetic &
Comparison>>Express Numeric palette, on the
block diagram. This function produces a random number
between
Place the Multiply function, located on the
Functions>>Arithmetic & Comparison>>Express
Numeric palette, on the block diagram. This
function multiplies the random number by
Right-click the y terminal of the
Multiply function, select
Create>>Constant from the shortcut menu,
type 10000, and press the
<Enter> key to create a numeric
constant.
Round To Nearest function,
located on the Functions>>Arithmetic &
Comparison>>Express Numeric palette, on the
block diagram. This function rounds the random number to
the nearest integer.
Not Equal? function, located on
the Functions>>Arithmetic &
Comparison>>Express Comparison palette, on the
block diagram. This function compares the random number
with Number to Match and returns
True if the numbers are not equal;
otherwise, it returns False.
Place the While Loop, located on the
Functions>>All Functions>>Structures
palette, on the block diagram. Right-click the
conditional terminal and select Continue if
True from the shortcut menu.
While Loop with Stop
button, located on the Execution Control
palette, when you need a stop button to stop the
While Loop. In this exercise, you use
the Number to Match control to stop the
loop; you do not need a Stop button.
Wire the iteration terminal to the border
of the While Loop. A blue tunnel appears
on the While Loop border. You will wire the
tunnel to the Increment function. Each time
the loop executes, the iteration terminal increments by
one. The iteration count passes out of the loop upon
completion. Increment this value by one outside the
loop because the count starts at
Increment function, located on
the Functions>>Arithmetic &
Comparison>>Express Numeric palette, on the block
diagram. This function adds
While
Loop count. A coercion dot appears on the
Number of iterations output to indicate that
LabVIEW coerced the numeric representation of the
iteration terminal to match the numeric representation
of the Number of iterations output. Refer
to the For Loops section for
more information about numeric conversion.
Auto Match.vi.
Number to Match.
Number to Match and run the
VI again. Current Number updates at every
iteration of the loop because it is inside the
loop. Number of iterations updates upon
completion because it is outside the loop.
To see how the VI updates the indicators, enable execution
highlighting. On the block diagram toolbar, click the
Highlight Execution button, shown at left, to
enable execution highlighting. Execution highlighting shows
the movement of data on the block diagram from one node to
another so you can see each number as the VI generates it.
Number to Match to a number that is out
of the data range, which is
"A full introductory course on programming with LabVIEW."