Scripting > mrScriptBasic overview > mrScriptBasic language reference > Statements > For...Next
 
For...Next
Repeats a group of statements a specified number of times.
Syntax
For counter = start To end [Step step] [{Ran|Rot|Rev} [it]]
[statements]
[Exit For]
[statements]
Next
Arguments
counter
Numeric variable used as a loop counter.
start
Initial value of counter.
end
Final value of counter.
step
Amount counter is changed each time through the loop. If not specified, step defaults to one.
it
The maximum number of iterations through the loop.
statements
One or more statements between For and Next that are executed the specified number of times.
Remarks
The step value is always expressed as a number, even when the counter is Categorical. The step argument can be either positive or negative. The value of the step argument determines loop processing as follows:
Value
Loop executes if
Positive
counter <= end
Negative
counter >= end
An error will be raised if the step value is 0.
Like the Step option, Ran, Rot, and Rev control how the counter variable is updated for each loop iteration. The Ran option causes counter to be assigned a random value between start and end. The Rot option causes counter to be assigned a rotated value between start and end and Rev causes counter to be assigned the reversed value on alternate executions. The Ran, Rot, and Rev options ensure that counter is only assigned each value once. Randomization and rotation is based on the Ran/Rot/Rev seed for the script. Optionally, the maximum number of randomized, rotated or reversed iterations can also be specified as it.
Once the loop starts and all statements in the loop have executed, step is added to counter. At this point, either the statements in the loop execute again (based on the same test that caused the loop to execute initially), or the loop is exited and execution continues with the statement following the Next statement.
Exit For can be used within a For...Next or For Each...Next control structure to provide an alternate way to exit. Any number of Exit For statements may be placed anywhere in the loop. Exit For is often used with the evaluation of some condition (for example, If...Then), and transfers control to the statement immediately following Next.
Examples
The following example illustrates using a For...Next statement to loop through the VariableInstance objects in an MDM Document, writing their English (United States) question texts to a text file. To run this example using the mrScript Command Line Runner, you need to use the /m: option to specify an .mdd file.
Dim fso, txtfile, i
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtfile = fso.CreateTextFile("variables.txt", True)

For i = 0 To MDM.Variables.Count - 1
txtfile.WriteLine(MDM.Variables[i].Labels["Label"].Text["Question"]["ENU"])
Next
The next example illustrates using a For...Next statement to set up random numbers in an array variable using the RanInt function.
Dim i, random[10]
For i = 0 To 9
random[i] = RanInt()
Next
Ask the first 4 iterations in the loop question:
Dim Counter
For Counter = 0 To 3
Rating[Counter].Wine.Ask()
Next
Ask alternating iterations in the loop question:
Dim Counter
For Counter = 0 To Rating.UBound() Step 2
Rating[Counter].Wine.Ask()
Next
Ask each iteration and exit when REF answered:
For Counter = 0 To Rating.UBound()
Rating[Counter].Wine.Ask()
If Rating[Counter].Wine = {REF} Then
Exit For
End If
Next
See also
Statements