Do Loops in Scripts

Do 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.  Do loops will be familiar to FORTRAN programmers (although you do not need to be familiar with that language to use them). They require an initial integer value for the loop variable, an end value for the variable and an integer defining how the loop variable is incremented.

A Do loop can be inserted by selecting “DO Loop” from the Script element’s Insert menu (or pressing Ctrl+O 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.

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

Increment by. This is the increment of the loop variable.  It must be specified as a dimensionless scalar value.  It is rounded to the nearest integer value.

At the beginning of the first loop, the total number of loops to be carried out is computed as follows:

(End Value  - Start Value + Increment by)/ Increment by

Three points should be noted here:

   If the number of loops computed above is negative, no loops are carried out;

   If Increment by is positive and Start Value > End Value, no loops are carried out; and

   If Increment by is negative and Start Value < End Value, no loops are carried out.

   Note: Because the number of loops is computed at the beginning of the first loop, changing the Loop Variable in the middle of the loop has no impact on the number of loops carried out.

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

   Note:  When the loop variable is referenced (i.e., in 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 “locally available” parts of the model. 

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

Note that the first line indicates the start value, the end value, 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 10. Hence, if the model had 10 timesteps, at the end of the simulation, Result would equal 110, since the loop would be evaluated 11 times (the loop is evaluated at Etime = 0 and every subsequent timestep).

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 multiple statements are selected (by left-clicking and dragging the line numbers) when the Do loop is first inserted (and the selection is a logically complete block of code), GoldSim will “wrap” the selection in the loop. 

   Warning: Unlike For loops, in a Do loop, the loop variable should generally not be assigned values within the loop. If it is, it will have no effect on the number of loops since the original increment logic is restored at the bottom of each loop.

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

One key property of Do 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: