<< Chapter < Page | Chapter >> Page > |
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.
[link] shows the Loop Condition set to Continue if True .
Programmatically, while loop shift registers are identical to for loop shift registers . Refer to [link] 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 [link] .
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 [link] .
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 [link] .
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 [link] . The execution of this program shows the time in milliseconds it took for the 2 nd 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 [link] ).
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 [link] .
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 [link] . 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 [link] and [link] are programmatically identical.
Notification Switch
Would you like to follow the 'Introduction to g programming' conversation and receive update notifications?