Summary: The creation of this content was supported in some part by NSF grant 0538934.
The case structure allows data to flow based on a integer, Boolean or string matching condition. The case executed is selected based on the data wired to the Case Selector.
![]() |
In the Front Panel window, select a Boolean control and an output string.
![]() |
Arrange the diagram to look as in Figure 3.
![]() |
In the True case, add a string constant containing True Case.
![]() |
To select the False case, click on the selector label down arrow and select False from the pop-up menu. You can also cycle through the cases by clicking the next (right) or previous (left) arrows.
![]() |
In the False case, add a string constant containing False Case.
![]() |
Wire the string constant in the case structure to the output string terminal.
![]() |
Select the True case and wire the string constant to the case structure tunnel. Complete the diagram as shown in Figure 8.
![]() |
It is important to note that all instances in a case structure must be wired to enable data to flow from the case structure.
In the Front Panel window, toggle the Boolean input control and run the program.
![]() |
![]() |
Select an Integer 32 numeric input and an Integer 32 numeric output and label them Selector and Case respectively.
![]() |
In the Block Diagram window, create a case structure, select the False case and arrange the terminals as shown in Figure 12.
![]() |
Wire the Selector numeric control to the case selector on the case structure. The selector label reflects the diagram update.
![]() |
In the 0, Default case, add a numeric constant and leave its value as 0.
![]() |
Using the selector label, select case 1. Add a numeric constant, enter 1 and wire it to the case tunnel. The resulting diagram is shown in Figure 15.
![]() |
Right click anywhere in the case structure and select Add Case After from the pop-up menu.
![]() |
Case 2 is added after case 1. Add a numeric constant, enter 2 and wire it to the case structure tunnel.
![]() |
Figure 18 shows the results of running this simple case selection programs for Selector set to 0, 1, 2 and 3 respectively.
![]() |
The For Loop structure repeatedly executes the diagram within the structure. The Loop Count specifies the number of times the loop contents must be executed and the Loop Iteration indicates which iteration is currently being executed.
![]() |
The Loop Count and Loop Iteration are of Integer 32 data types. If the Loop Count is set to N, then the Loop Iteration value range is from 0 to N-1. This is illustrated in Figure 20 and Figure 21.
![]() |
Shift Registers allow the preservation of intermediate results between sequences of iterations.
![]() |
![]() |
To add a Shift Register, right click on the For Loop structure and select Add Shift Register from the pop-up menu.
![]() |
To add elements to the shift register, right click on the shift register and select Add Element from the pop-up menu.
![]() |
![]() |
To illustrate the use of the shift registers, the following example computes the Fibonacci number Fib(n).
In the Front Panel window, select an integer 32 numeric input and output controls and labeled them n and Fib(n) respectively. Arrange the diagram as shown in Figure 27.
|
Add a 0 and 1 numeric constants to initialize the elements of the shift register and wire them to the i-1 and i-2 elements respectively. Add the add operator in the for loop and complete the program wiring as shown in Figure 28.
![]() |
For n = 0, the for loop iterates 0 times and passes 0 to Fib(n), therefore Fib(0)=0. For n = 1, the for loop the values in i-1 and i-2 shift register elements are added (0+1) and saved in the i shift register element (1). Since the loop iterates once only, the resulting value is passed to Fib(n), therefore Fib(1)=1. For n = 2, the first iteration produces the value of 1. Prior to the next and final iteration, the values are shifted in the register as follows:
The value in the i-2 shift register element is discarded
The value in the i-1 shift register element is shifted to the i-2 shift register element
The value in the i shift register element is shifted to the i-1 shift register element
To start the 2nd and final iteration, the i-1 shift register element contains 1 and the i-2 shift register element contains 0. These are added to produce 1, which is passed to Fib(n) and, therefore, Fib(2)=1. This process is repeated for values of n > 2.
Save this program as Fibonacci.vi. Figure 29 shows the result of Fib(8).
Auto-indexing allows input array elements to be operated on and output array elements to be aggregated automatically in a for loop. It is not required to wire the Loop Counter. The for loop automatically reduces the array dimensionality by one.
![]() |
It is sometimes necessary to disable auto-indexing. In this example, the For Loop is used to scan the elements of the array taking advantage of the auto-indexing feature. However, the result is a single number. Wiring the result through the For Loop with auto-indexing enabled results in a broken data type wire.
![]() |
To disable auto-indexing, right click on the target Auto-Indexed Tunnel and select Disable Indexing from the pop-up menu.
![]() |
The final diagram with the Auto-Indexed Tunnel disabled is shown in Figure 33.
![]() |
The While Loop conditionally iterates executing the statements within the structure. The Loop Condition establishes whether the loop iterates or terminates. The Loop Iteration is a zero-based iteration execution reference similar to the For Loop.
![]() |
The default loop condition is to continue if the Boolean condition is False (or stop if True). The while loop in Figure 35 will iterate while Iterations is less than Loop Iteration is False or, equivalently, will stop iterating when Iterations is less than the value in Loop Iteration.
![]() |
At times it is more convenient to let the while loop iterate while the condition is True. To change the loop condition, right click on the loop condition icon and select Continue if True from the pop-up menu.
![]() |
Figure 37 shows the Loop Condition set to Continue if True.
![]() |
Programmatically, while loop shift registers are identical to for loop shift registers. Refer to Section 5 for the discussion. However, an example is provided to illustrate the use of shift registers in while loops.
![]() |
In the following example, Euler’s number e is computed to the specified accuracy using the infinite series
Notice that two shift registers keep track of the factorial and the sum. Also notice the dot in the multiplication. This is because the loop iteration is an integer 32 data type and the input from one of the shift registers is double precision numeric. The dot represents that the integer 32 data type has been coerced into a double precision number.
![]() |
Save the program as e.vi. The result of running this program is shown in Figure 40.
![]() |
By default, while loops are auto-indexed disabled. In order for while loops to process and generate arrays, the loop tunnel must be enabled to auto-indexed arrays.
![]() |
To enable auto-indexing, right click on the loop tunnel and select Enable Indexing from the pop-up menu.
![]() |
In this example the while loop appropriately generates a 1,000 element numeric array with random numbers.
![]() |
Although G was designed to easily develop interactive, parallel programs, it is sometimes necessary to execute diagrams in sequential order. The sequence structure allows G diagrams to execute sequentially.
The following examples time in milliseconds (ms) the execution of a G diagram. The sequence of events is get a start time stamp, execute the diagram, get stop time stamp and take the difference between the stop and start times to determine the execution time.
![]() |
Flat Sequences always execute left to right. A Flat Sequence structure starts with a single frame and allows a user to visualize the diagram sequences.
![]() |
To add frames to a sequence, right click on the sequence structure and select either Add Frame After or Add Frame Before from the pop-up menu according to the program’s needs.
![]() |
Add two more frames to the sequence structure to get a three frame sequence as shown in Figure 47.
![]() |
From the Functions >> Programming >> Timing menu select Tick Count (ms) function.
![]() |
Drop the Tick Count (ms) function in the first (left most) frame of this sequence. Make a copy of the Tick Count function and place it on the third (right most) frame as shown in Figure 49.
![]() |
Add a For Loop that iterates 5,000 times to the second frame. Add a subtract operator, an unsigned integer 32 output and complete the program as shown in Figure 50. The execution of this program shows the time in milliseconds it took for the 2nd sequential frame to execute.
![]() |
A Stacked Sequence provides a more compact representation of program sequences. It is programmatically identical to the Flat Sequence with the exception that a Sequence Local enables data to flow to subsequent frames. Additionally, as frames are added, a Sequence Selector provides access to the desired frame (see Figure 51).
![]() |
For this timing example, start with a Stacked Sequence and add 3 more frames. The sequence frames are labeled 0, 1, 2 and 3 and will execute in that order.
![]() |
Go to the first frame (frame 0) and add a Tick Count (ms) function. Right click on the sequence structure and select Add Sequence Local from the pop-up menu.
![]() |
![]() |
The Sequence Local is shown as an undefined tunnel. Wire the Tick Count (ms) function to the Sequence Local to define the tunnel data type and data flow. Data can now flow from frame 0 to the other frames as needed.
![]() |
![]() |
Go to the next frame sequence (frame 1) and enter the program to be timed.
![]() |
Go to the third frame of the sequence (frame 2), add a Tick Count (ms) function, add another Sequence Local and wire the Tick Count (ms) to the new Sequence Local. The wired sequence frame is shown in Figure 58.
![]() |
Go to the last frame (frame 3) and add a Subtract function. Wire the Sequence Locals from frame 2 and frame 0 to the Subtract function as shown in Figure 59. To complete the diagram, wire the output of the Subtract function to the unsigned integer 32 output.
![]() |
It is important to note that the programs in Figure 50 and Figure 59 are programmatically identical.