Developer Documentation Library > Scripting > mrScriptBasic overview > mrScriptBasic language reference > Statements > Do...Loop
 
Do...Loop
A Do...Loop structure repeats a block of statements while a condition is True or until a condition becomes True.
Syntax
Do [{While | Until} expression]
[statements]
[Exit Do]
[statements]
Loop
Do
[statements]
[Exit Do]
[statements]
Loop [{While | Until} expression]
Arguments
expression
Any expression that can be evaluated to return a result. The result type of the expression will be coerced to True or False.
statements
One or more statements that are repeated while or until the expression is True.
Remarks
The Do...Loop statement can be used in many different ways. By placing the While or Until expression after the Do, the expression condition is checked prior to any iterations. By putting the While or Until expression after the Loop statement, a single iteration is guaranteed.
The Exit Do is used in a Do...Loop control statement to provide an alternative way to exit a Do...Loop. Any number of Exit Do statements can be put anywhere in the Do...Loop. Often used with the evaluation of some condition (for example, If...Then), Exit Do transfers control to the statement immediately following the Loop.
When used within nested Do...Loop statements, Exit Do transfers control to the loop that is nested one level above the loop where it occurs.
Examples
The following examples use a Do...Loop 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.
This first example keeps writing the question texts until the counter is equal to the Variables.Count property, which stores the number of VariableInstance objects in the collection:
Dim fso, txtfile, Counter
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtfile = fso.CreateTextFile("variables.txt", True)

Counter = 0
Do
txtfile.WriteLine(MDM.Variables[Counter].Labels["Label"].Text["Question"]["ENU"])
Counter = Counter + 1
Loop Until Counter = MDM.Variables.Count
Here is an alternative construction for the loop:
Counter = 0

Do Until Counter = MDM.Variables.Count
txtfile.WriteLine(MDM.Variables[Counter].Labels["Label"].Text["Question"]["ENU"])
Counter = Counter + 1
Loop
Here is another alternative:
Counter = 0
Do While Counter < MDM.Variables.Count
txtfile.WriteLine(MDM.Variables[Counter].Labels["Label"].Text["Question"]["ENU"])
Counter = Counter + 1
Loop
Keep asking until the answer is not REF:
Do
Location.Ask()
Loop While Location = {REF}
An alternative construction is:
Do
Location.Ask()
Loop Until Location <> {REF}
Another alternative, that exits after 5 asks:
Dim Counter
Counter = 0
Do
Location.Ask()
Counter = Counter + 1
If Counter = 5 Then
Exit Do
End If
Loop While Location = {REF}
See also
Statements