<< Chapter < Page Chapter >> Page >
Three elements comprise this diagram. From left to right there is an icon labeled 'Iterations' connected via a line to a large box containing an 'i' in a blue box and a triangle containing '<' which is connected to a green square red circle. The 'i' is connected via a line to an icon outside the large box labeled 'count'.
Stop If True

Continue if true

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.

Three elements comprise this diagram. From left to right there is an icon labeled 'Iterations' connected via a line to a large box containing an 'i' in a blue box and a triangle containing '<' which is connected to a green square red circle. The 'i' is connected via a line to an icon outside the large box labeled 'count'. There is a menu overlaid on top of the diagram and the menu item 'Continue if True' is highlighted, while the item 'Stop if True' has a check mark next to it.
Changing Loop Condition

[link] shows the Loop Condition set to Continue if True .

A diagram consisting three icons. From left to right the icons are a blue square labeled 'Iterations'. There is large box to the right containing a blue square 'i', a triangle containin a sideways 'w', a green square containing a circular arrow. On the other side of the large box is an icon labeled 'Count'.
Continue If True

Shift registers

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.

A box with two black down arrow boxes on the far right of the box, blue box 'i' on the lower left of the box, and up arrow on the upper right of the box and a green square with a circular arrow in the lower right of the box.
While Loop Shift Registers

In the following example, Euler’s number e is computed to the specified accuracy using the infinite series

e = n = 0 n = 1 n ! = 1 + n = 0 n = 1 n ! = 1 + 1 1 ! + 1 2 ! + 1 3 ! + = 2.7182818284

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.

A complex diagram of 'Computing e'. It consists of an icon on the right labeled 'Accuracy' connected to a large box diagram with many triangles and squares. There is also a square icon on the left labeled 'e'.
Computing e

Save the program as e.vi . The result of running this program is shown in [link] .

A form with two fields labeled 'Accuracy' and 'e'. The values of the fields are '1E-5' and '2.71828'respectively.
Computed e to 5 Digits

Enabling auto-indexing

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.

A big square box contain a blue square 'i' and a blue square '999' connected via lines to a triange containing a '<'. This triangle is connected to a green square containing a circular arrow. Above these icons is a dice icon which connects to an orange box on the edge of the big box. To the right there is a red 'x' and then an orange box around an 'array' icon.
Disabled Auto-Indexing

To enable auto-indexing, right click on the loop tunnel and select Enable Indexing from the pop-up menu.

A big square box contain a blue square 'i' and a blue square '999' connected via lines to a triange containing a '<'. This triangle is connected to a green square containing a circular arrow. Above these icons is a dice icon which connects to an orange box on the edge of the big box. To the right there is a red 'x' and then an orange box around an 'array' icon. On top of this diagram, a menu is overlaid with the item 'Enable Indexing' highlighted in blue.
Enabling Auto-Indexing

In this example the while loop appropriately generates a 1,000 element numeric array with random numbers.

A big square box contain a blue square 'i' and a blue square '999' connected via lines to a triange containing a '<'. This triangle is connected to a green square containing a circular arrow. Above these icons is a dice icon which connects to a hollow box on the edge of the big box. To the right there is an orange box around an 'array' icon.
Auto-Indexing Enabled

Sequence

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.

A diagram that looks like two frames of film. The left frame is labeled 'Execute First' and the right frame is labeled 'Execute Next'.
Sequence Structure

Flat sequence

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.

An empty film frame.
Sequence Frame

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.

An empty film frame overlaid by a menu with the item 'Add Frame After' highlighted in blue.
Adding Sequence Frames

Add two more frames to the sequence structure to get a three frame sequence as shown in [link] .

A row of three empty film frames.
Three Frame Sequence

From the Functions>>Programming>>Timing menu select Tick Count (ms) function.

A row of three icons is nested in the second level of a directory. The levels are labeled 'Programming' and 'Timing' underneath that. The icons are labeled from left to right 'Tick Count (ms)', 'Wait(ms)' and 'Wait Until Ne...'.
Tick Count 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] .

A diagram of three film frames with the left and right most frames contain clock icons.
Start and Stop Tick Counts

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 diagram of three film frames with the left and right most frames contain clock icons. In the middle fram there is an icon with a blue square on the left containing '5000' which is connected to a square containing a blue square 'N' in the upper left and a blue square 'i' in the lower left and dice on the middle. A line runs for the first clock to a triangle containing a '-' on the right of the film frames and then the triangle connects to a square icon.
Timing G Program

Stacked sequence

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] ).

A film frame with '0[0..1]' with arrows on either side pointing to the right and a down error.
Stacked Sequence

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.

A film frame with '3[0..3]' with arrows on either side pointing to the right and a down error.
Four Frame Stacked Sequence

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.

A film frame with '3[0..3]' with arrows on either side pointing to the right and a down error. A clock icon is contained in the middle of the frame.
Adding Sequence Locals
A film frame with '3[0..3]' with arrows on either side pointing to the right and a down error. A menu is overlaid on top of this diagram with the item 'Add Sequence Local' is highlighted in blue.
Adding Sequence Locals

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.

A film frame with '3[0..3]' with arrows on either side pointing to the right and a down error. There is a clock icon in the middle of the frame with a hollow square on the lower left.
Sequence Local
A film frame with '3[0..3]' with arrows on either side pointing to the right and a down error. There is a clock icon in the middle of the frame connected to a square containing a blue arrow on the lower left via a line.
Sequence Local

Go to the next frame sequence (frame 1) and enter the program to be timed.

A film frame with '1[0..3]' with arrows pointing either direction and a down arrow at the top of the frame. Inside the frame there is an icon with a blue square on the left containing '5000' which is connected to a square containing a blue square 'N' in the upper left and a blue square 'i' in the lower left and dice on the middle. There is also a square arrow pointing to the right.
Frame to Time

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] .

A film frame with '2[0..3]' with arrows pointing either direction and a down arrow at the top of the frame. There is a clock icon in the middle of the frame with an arrow pointing left and another pointing right on the left wall of the frame.
Stop Time Stamp

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.

A film frame with '3[0..3]' with arrows pointing either direction and a down arrow at the top of the frame. Contained within the frame from left to right there are two blue square arrows pointing right with a blue line connecting to a triangle box containing '-' connected to a blue square on the left edge of the frame wall. A line connects this box to a blue box on the outside of the frame.
Stacked Timing G Program

It is important to note that the programs in [link] and [link] are programmatically identical.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Introduction to g programming. OpenStax CNX. Mar 15, 2010 Download for free at http://cnx.org/content/col11192/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Introduction to g programming' conversation and receive update notifications?

Ask