| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (18 out of 25)

for

This statement instructs G2 to repeatedly execute a statement or sequence of statements enclosed by do and end. You determine the terminating conditions. You can execute for statements for each instance of a class of items, for each item or value in a list or array, or based on the value of a numeric expression.

Iterating Over Each Instance of a Class of Items

You can use the for statement with the each quantifier to execute a sequence of statements once for each instance of a class of items. The syntax is:

where:


Specifies any generic reference to an item or value that G2 iterates over in the set.

Any procedure or action statement.

The for loop terminates when all the instances of the specified class have been processed. For example:

This example changes the icon-color used for each instance of the class valve. To limit the iteration, you could use the following:

In this example, only instances of valves connected to tank-1 change color.

Iterating Using a Counter

You can use a counter to execute a sequence of statements based on the value of the counter, as you increment or decrement it. The syntax is:

where:


A local name that represents the iteration variable or counter. G2 assigns numeric-expression-1 as its initial value. By default, the counter is incremented by 1 or the specified increment (if you specify to) or decremented (if you specify down to).

Any expression that evaluates to a number.

Specifies that the counter is incremented by 1.

Specifies that the counter is decremented by 1.

Any expression that evaluates to a number that is not equal to the value of numeric-expression-1.

Specifies that the counter is either incremented or decremented, depending on which syntax you use, by a specified number (increment).

Each time G2 executes the for statement, it compares the value of the counter with the values of numeric-expression-1 and numeric-expression-2. If the counter value is equal to or within the range specified by numeric-expression-1 and numeric-expression-2, G2 executes the statements contained in the body.

It then increments or decrements the counter, depending on whether you specified to or down to. By default, the counter is incremented by 1 (if you specify to) or decremented by 1 (if you specify down to). You can also specify a different increment or decrement by using the optional by increment phrase.

On each repetition, G2 only executes the body of the for statement after comparing the counter with numeric-expression-1 and numeric-expression-2. If the counter value is outside the specified range, the loop is completed, and G2 passes control to the statement immediately following the for statement.


Note: You can also transfer control out of the for loop by using an exit if statement. See exit if for more information.

An example is:

Here, the last iteration of the body of the for will be performed when loop-counter equals 99.

For Each and Wait States

A for each statement does not in itself cause a procedure to enter a wait state. However, executing a statement of the form for each ... do in parallel causes a procedure to enter a wait state between the iteration which launches the parallel iterations and the entrance to each parallel iteration. This enables an arbitrary number of threads to be launched through the iteration without risk of the procedure timing out.

For information about wait states see Allowing Other Processing. The do in parallel statement is described under do in parallel.


Caution: When a procedure returns from a wait state, the environment in which it executes may have changed in ways that invalidate the procedure's assumptions. Be sure to revalidate the environment as needed before continuing execution of the procedure.

| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (18 out of 25)

Copyright © 1997 Gensym Corporation, Inc.