San Diego State University logo
ME 205

The Simulation of Physical Systems

 

LabView Tutorial 3 - Loops



The While Loop

Definition

A While Loop executes all or a portion of your Block Diagram code multiple times.

A While loop is equivalent to the following pseudo-code:

Do

Execute diagram Inside the loop

While the condition is TRUE/FALSE


How to Create a While Loop

You select the while loop by popping-up in the
Block Diagram > Functions > Structures > While Loop:

The 'While Loop' menu options

You then drag a rectangle area around the portion of Block Diagram code that you want to be included in the while loop:

Dragging an area containing the part of the Block Diagram to loop over.

When you let go of the cursor, you will see the while loop encompassing the code to be included in the while loop:

The area indicated is highlighted with a 'While Loop' icon in the lower right, and an 'Iteration' icon in the lower left.


While Loop Components:

The While Loop

    The While Loop encompasses the Block Diagram code. The loop area is re-sizeable; just select the border and drag. You can also add or remove Block Diagram nodes and elements to and from the while loop by dragging or dropping them inside or outside.

The Conditional Terminal

The conditional terminal has two possible states.

The 'Continue if True' icon This is the default Continue if True condition. This means that the while loop will continue to repeat the code until a boolean value of FALSE is passed to the conditional terminal.

The 'Stop if True' icon This is the Stop if True condition. This means that the while loop will continue to repeat the code until a boolean value of TRUE is passed to the conditional terminal. You change the conditional terminal to this condition by popping-up on the terminal and selecting Stop If True from the pop-up menu:

The 'Conditional Terminal' pop-up menu, with 'Stop if True' selected

You can switch back to the Continue if True condition by reversing the above procedure:

The 'Conditional Terminal' menu, with 'Continue if True' selected

The Iteration Terminal

The The Iteration Terminal is a terminal that outputs the number of times the loop has been executed. The count starts at zero (the first iteration of the loop is Iteration Zero). This is often wired to an indicator terminal to keep track of the number of iterations; it can also be utilized to increment data in the loop.


Controlling a While Loop

Where does the boolean value come from? Generally the conditional terminal is wired to a Boolean Terminal. This terminal represents a Boolean Control (switch) on the Front Panel for the user to control the operation of the while loop.

Front Panel & Block Diagram with the Boolean Control highlighted on both.


The For Loop

Definition

The For Loop repeats Block Diagram code a predetermined number of counted times. When the number of iterations equals the predetermined count, the loop stops.

A For Loop is equivalent to the following pseudo-code:

For i = 0 to N-1

Execute diagram inside the loop


How to Create a For Loop

You select the for loop by popping-up in the
Block Diagram > Functions > Structures > For Loop:

The 'For Loop' menu options, in the 'Structures' sub-menu of the Block Diagram's 'Functions' pull-down menu.

 

You then drag a rectangle area around the portion of Block Diagram code that you want to be included in the for loop:

Dragging across an area of the 'Block Diagram' to include in the 'For' loop.

When you let go of the cursor, you will see the for loop outline encompassing the code to be included in the for loop:

The selected area is highlighted as a 'For' loop, with a 'Count' terminal in the upper left, and an 'Iteration' terminal in the lower left.


For Loop Components:

The For Loop

The for loop itself encompasses the Block Diagram code. The loop area is re-sizeable. Just select the border and drag. You can also add or remove Block Diagram nodes and elements to and from the for loop by dragging or dropping them inside or outside.

The Count Terminal

The The Count Terminal is where the predetermined count is placed. Often, a control terminal or a numeric constant is wired to the count terminal to control the number of iterations.

The Iteration Terminal

The The Iteration Terminal in a for loop is much like the iteration terminal in a while loop. It is a terminal that outputs the number of times the loop has been executed. The count starts at zero (the first iteration of the loop is Iteration Zero) This is often wired to an indicator terminal to keep track of the number of iterations; as with the while loop above, it can also be utilized to increment data in the loop.

Proceed to the Review Section ->