From the course: Learning LabVIEW

While loops - LabVIEW Tutorial

From the course: Learning LabVIEW

Start my 1-month free trial

While loops

There will often be times when you need your program to repeat an action or a section of code multiple times. And we can accomplish that in LabView using loops. To demonstrate that, I'll create a simple program to generate a random number. I'll bring up the functions palette, go to the numeric menu and select this random number function, place it and then I'll right click on the output and create an indictor. Now this program will generate a new random number every time I run it. But, let's say I want this program to continue generating new numbers over and over until the user tell the program to stop, to repeat that action, I'll open the functions palette, go to the structures menu and then select while loop. Placing a while loop on the block diagram works differently than other functions, I'll left click my mouse and while holding it, I'll drag the mouse to select the section of code I want to repeat and then when I release my mouse, it creates the while loop. It encloses that code in this loop which has a solid gray border with a tiny arrow in the corner to indicate looping. A while loop will continuously execute the code inside of it over and over until it's told to stop. Each iteration of the while loop will completely execute all of its code then check to see if it's been told to stop. If it has not been told to stop yet then it will repeat that process again. Otherwise, if it has been signaled to stop then the loop will exit. There are two objects that are part of the while loop that we can use when programming it. The most important one is this little, red stop sign in the corner because it has to be connected for the program to be runnable. The stop terminal is surrounded with a green border to indicate that it takes Boolean value as input. As long as that input value is false, the loop will continue running forever but when you change it to true, the loop will exit after it finishes executing the current iteration. Your program may have some logic and code to determine when it should stop but for this example, I'll simply right click on the stop terminal input, create a control and that will allow the user to manually stop it. Notice that the button that LabView creates says stop on it, so, there's no doubt about what it does. The other object in the while loop is a blue box with the letter I on it. This box outputs an integer value indicating the number of iterations the loop has made. During the first iteration, the value will be zero then it gets incremented to one for the next iteration and so on. I'll right click and create a indictor so we can see the output value from it. And I'll line that up with the other indicators and center them on my front panel. Now, I'll click the regular run arrow to start the VI and unlike the previous examples, this VI continuously runs on its own. I did not click the run continuously button. Notice that the run arrow has turned black. I can't select it because the program is already running and it's running fast. This loop is iterating very quickly, generating a new random number each round. And you can see the iteration counter increasing rapidly as well. To stop this program, rather than clicking the stop icon, which will cause it to abruptly abort, I'll click the stop button. After the loop finishes it's current iteration, the loop exits and then the program finishes.

Contents