For Loops in Scripts

For loops are one of four different types of loops provided in the Script element.  They are used to loop through a set of statements.  Common applications are defining and manipulating arrays, and doing an iterative calculation.  For loops will be familiar to C/C++ programmers (although you certainly do not need to be familiar with these languages to use them). They require an initial integer value for the loop variable, a “Loop while true” condition, and an integer defining how the loop variable is incremented.

A For loop can be inserted by selecting “FOR Loop” from the Script element’s Insert menu (or pressing Ctrl+F when in the Script dialog). The block is inserted below the statement that was selected when this is done.  The following dialog will be displayed:

The first field is the name of the loop variable.  This becomes a local variable within the loop (identical to having used a Variable Definition statement to create a variable).

   Note: This cannot be the name of an existing script variable.  You must define a new variable here.

The remaining three fields are expression fields (i.e., they can be defined as an expression):

Start Value.  This is the initial value of the loop variable.  It must be specified as a dimensionless scalar value.  It is rounded to the nearest integer value.

Loop while true. This must be specified as a scalar condition. This condition is evaluated at the beginning of each loop (including the first loop). If it is True, the loop is executed.  If it is false, control drops out the bottom of the loop.

Increment by. After a loop is completed, if the “Loop while true” condition is True, the loop variable is incremented by this value before returning to the top of the loop. It must be specified as a dimensionless scalar value (it can be positive or negative).  It is rounded to the nearest integer value.

   Note: If you do not change the default settings, the loop will be carried out 10 times.

   Note:  When the loop variable is referenced (either in the loop definition or any statements that are subsequently added to the contents of the loop), it is referenced using the ~ operator.  This is because the loop variable, like all local script variables, is a locally available property.  Locally available properties are only available in specific (local) parts of the model. 

After defining the properties of the For loop, the script will look like this:

Note that the first line indicates the start value, loop while true condition, and the increment.

Obviously, such a loop serves no function unless it contains some statements inside the looping block. To insert a statement inside the block, you would select the first part of the block (in this case, line 1), and insert a statement.  In the example below, a Variable Assignment statement has been inserted:

In this case, every time the Script element was updated, Result would be incremented by 1.

You can also move existing statements that are above or below the loop into the loop by using the buttons at the bottom of the Script dialog.

   Note: If one or more statements are selected (by left-clicking and dragging the line numbers) when the For loop is first inserted (and the selection is a logically complete block of code), GoldSim will “wrap” the selection in loop. 

Because the loop variable is simply a local script variable, it can also be assigned values within the loop (i.e., in addition to being incremented at the end of each loop).  For example, this loop would be interrupted if A = B:

For loops can have any number of statements within them.  For example, a For loop could contain other nested loops, as well as complex if, then logic.  Here is an example of a For loop with a nested For loop and an If-Else block:

One key property of For loops that must be understood is that the loop creates a local scope for any variables that are defined within it.  Hence, the loop variable (and any other variables defined in the loop) could not be referenced outside of the loop (without first redefining them).

Related Topics…

Learn more about: