Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI

Document Type: Tutorial
NI Supported: Yes
Publish Date: Jul 2, 2008


Feedback


Yes No

Related Categories

Products

Development Topic

Related Links - Developer Zone

Related Links - Products and Services

Exercise: For Loops and While Loops

0 ratings | 0.00 out of 5
Print | PDF

Overview

This exercise will show you the differences between while loops and for loops. You will go through the steps in creating each type of loop and how to use them.

Goal

Understand the basic behavior of For and While loops in LabVIEW.

Description

This exercise consists of a series of tasks in which will walk you through how to use For and While loops effectively in LabVIEW.

While Loops

Goal

Use a While Loop and an iteration terminal and pass data through a tunnel.

Scenario

Create a VI that continuously generates random numbers between 0 and 1000 until it generates a number that matches a number selected by the user. Determine how many random numbers the VI generated before the matching number.

Design

Type

Name

Properties

Input

Number to Match

Double-precision,  floating-point between 0 and 1000, coerce to nearest whole number, default value = 50

Output

Current Number

Double-precision, floating-point

Output

Number of Iterations

Integer

 

Implementation

Open a blank VI and build the following front panel. Modify the controls and indicators as shown in the following front panel and as described in the following steps.

1. Open a blank VI.

❑ Select File»New VI

2. Save the VI as Auto Match.vi.

3. Create the Number to Match input.

❑ Add a numeric control to the front panel window.

❑ Label the control Number to Match.

4. Set the properties for the Number to Match control so that the default value is 50, the data range is from 0 to 1000, the increment value is 1, and the digits of precision is 0.

❑ Right-click the Number to Match control and select Data Entry from the shortcut menu. The Data Entry page of the Numeric Properties dialog box appears.

❑ Set the Default Value to 50.

❑ Set the Minimum value to 0 and select Coerce from the Response to value outside limits pull-down menu.

❑ Set the Maximum value to 1000 and select Coerce from the Response to value outside limits pull-down menu.

❑ Set the Increment value to 1 and select Coerce to Nearest from the Response to value outside limits pull-down menu.

❑ Select the Display Format tab.

❑ Select Floating Point and change Precision Type from Significant digits to Digits of precision.

❑ Enter 0 in the Digits text box and click OK.

5. Create the Current Number output.

❑ Add a numeric indicator to the front panel window.

❑ Label the indicator Current Number.

❑ Right-click the indicator and select Advanced»Synchronous Display.

6. Set the digits of precision for the Current Number output to 0.

❑ Right-click the Current Number indicator and select Display Format from the shortcut menu. The Display Format page of the Numeric Properties dialog box appears.

❑ Select Floating Point and change Precision Type to Digits of precision.

❑ Enter 0 in the Digits text box and click OK.

7. Create the # of iterations output.

❑ Place a numeric indicator on the front panel.

❑ Label the indicator # of iterations.

8. Set the representation for the # of iterations output to a long integer.

❑ Right-click the # of iterations indicator.

❑ Select Representation»I32 from the shortcut menu.

 

Create the following block diagram. Refer to the following steps for instructions.

9. Generate a random number integer between 0 and 1000.

❑ Add the Random Number (0-1) function to the block diagram. The Random Number (0-1) function generates a random number between 0 and 1.

❑ Add the Multiply function to the block diagram. The Multiply function multiplies the random number by the y input to produce a random number between 0 and y.

❑ Wire the output of the Random Number function to the x input of the Multiply function.

❑ Right-click the y input of the Multiply function, select Create»Constant from the shortcut menu, enter 1000, and press the <Enter> key to create a numeric constant.

❑ Wire the output of the Multiply function to the input of the Round To Nearest function.

❑ Add the Round To Nearest function to the block diagram. This function rounds the random number to the nearest integer.

❑ Wire the output of the Round To Nearest function to the Current Number indicator.

10. Compare the randomly generated number to the value in the Number to Match control.

❑ Add the Not Equal? function to 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.

❑ Wire the output of the Round To Nearest function to the x input of the Not Equal? function.

❑ Wire the Number to Match numeric control to the y input of the Not Equal? function.

❑ Wire the output of the Not Equal? function to the conditional terminal.

11. Repeat the algorithm until the Not Equal? function returns True.

❑ Add a While Loop from the Structures palette to the block diagram.

❑ Right-click the conditional terminal and select Continue if True from the shortcut menu.

12. Display the number of random numbers generated to the user by adding one to the iteration terminal value.

❑ Wire the iteration terminal to the border of the While Loop. A blue tunnel appears on the While Loop border.

Tip: Each time the loop executes, the iteration terminal increments by one. Wire the iteration value to the Increment function because the iteration count starts at 0. The iteration count passes out of the loop upon completion.

❑ Add the Increment function to the block diagram. This function adds 1 to the While Loop count.

  1. Save the VI.

Test

1. Display the front panel.

2. Change the number in Number to Match to a number that is in the data range, which is 0 to 1000 with an increment of 1.

3. Run the VI.

4. Change Number to Match and run the VI again. Current Number updates at every iteration of the loop because it is inside the loop. # of iterations updates upon completion because it is outside the loop.

5. To see how the VI updates the indicators, enable execution highlighting.

❑ On the block diagram toolbar, click the Highlight Execution button 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.

6. Run the VI and observe the data flow.

7. Try to match a number that is outside the data range.

8. Change Number to Match to a number that is out of the data range.

❑ Run the VI. LabVIEW coerces the out-of-range value to the nearest value in the specified data range.

9. Close the VI.

For Loops

Implementation

1. Open a blank VI.

❑ Select File»New VI

2. Switch to the block diagram.

❑ Select Window»Show Block Diagram.

3. In the block diagram place a For Loop.

❑ From the functions palette navigate and select Programming»Structures»For Loop.
❑ Place the For Loop on the block diagram by clicking and dragging the mouse on the block diagram to form a square shape.

 

4. Set the number of iterations of the loop. 

❑ Right click the input of the Loop Count terminal of the For Loop and select Create»Constant.
❑ Give this constant a value of 10.

 

 

5. Place a Subtract function on the block diagram.

❑ From the functions palette navigate and select Programming»Numeric»Subtract.
❑ Place the Subtract function inside the for loop.

6. Create a constant on the input of the Subtract function.

❑ Right click the first input of the Subtract function and select Create»Constant.
❑ Give this constant a value of 10.

6. Create an indicator on the output of the Subtract function.

❑ Right click the first input of the Subtract function and select Create»Indicator.

8. Wire the Loop Iteration output of the For Loop to the second input of the Subtract function.

9. Add a Wait function to the block diagram.

❑ Select Programming»Timing»Wait from the functions palette and place it inside the for loop.
❑ Right click the  input of the Wait function and select Create»Constant.
❑ Give this constant a value of 1000.

10. Switch to the Front Panel.

❑ Select Window»Show Block Diagram.

11. Run the VI. Observe how the program counts down from 10 to 1 for each iteration of the For Loop.

13. Close the VI.

End Of Exercise

 


Tutorial           Video          Loops          Modules Home          FIRST Community

Downloads

for_loop_sol1.vi

while_loop_sol.vi

0 ratings | 0.00 out of 5
Print | PDF

Reader Comments | Submit a comment »

 

Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).