Desktop User Guides > Professional > Interview scripting > Writing interview scripts > Repetitive actions in the routing section > Repeat while or until a condition is true
 
Repeat while or until a condition is true
The Do...Loop statement lets you repeat a number of statements either all the time that an expression returns True or until an expression returns True. There are several ways you can write these loops.
While an expression is true
To write a loop that is repeated all the time that an expression returns True, use one of these commands:
Do While
  Expression Statements
Loop
or:
Do
  Statements
Loop While Expression
You can nest Do loops to any number of levels.
The difference between the two loop formats is this. If you place the expression at the start of the loop and it returns False, the statements inside the loop will never be executed. If you place the expression at the end of the loop, the statements inside the loop will always be executed at least once, because the expression is not tested until the end of the first pass through the loop.
Parameters
Expressions
A logical expression that controls whether the statements in the loop will be repeated.
Statements
The statements to be executed.
Example
This example shows how to use Do...Loop to ask a question and repeat it if the answer is not correct. The questions in the metadata section are:
HoursTV "How many hours, to the nearest quarter hour, did you spend
watching TV last week?" double [0 .. 168];
Programs "How much of that time was spent watching ..." loop
{
Films,
News "News programs",
Documentaries,
Sitcoms "Situation comedies",
Otherprogs "Other programs"
} fields
(
ProgTime "" double [0..168];
) expand grid;
NoMatch "The sum of the times you have just entered is {Total} but the
total time you gave earlier is {HoursTV}. Please check those figures."
info;
The routing section needs to do three things:
1 Ask the question.
2 Check that the sum of program times matches the total time.
3 If the sum of times does not match the total time, issue an error message and repeat the question.
The loop that you write is as follows:
HoursTV.Ask()
Dim Total, Prog
Do
Total=0.0
Programs.Ask()
' Add up the individual program times
For Each Prog in Programs
Total = Total + Prog.Item["ProgTime"]
Next
' Compare sum of program times with original total time
If Total <> HoursTV Then
NoMatch.Label.Inserts["Total"] = Total
NoMatch.Label.Inserts["HoursTV"] = HoursTV
NoMatch.Show()
End If
Loop While (Total <> HoursTV)
Besides showing how to write a Do While loop, this example is also interesting because it includes a For Each loop and an If block and shows how these statements can be combined to perform a common scripting task for numeric questions.
Until an expression becomes true
Repeating actions until an expression returns True is the opposite of Do While, but its syntax is identical except for the replacement of While with Until. As with Do While, there are two variants:
Do Until Expression
  Statements
Loop
or:
Do
  Statements
  [Exit Do]
  More statements
Loop Until Expression
In both cases:
Expression is a logical expression that controls whether the statements in the loop will be repeated.
Statements are the statements to be executed.
If you want to execute the statements in the loop at least once, place the expression at the end of the loop. Here is the earlier example rewritten to use Until. Notice that it identical apart from the last line, where Until replaces While and the operator in the expression changes from <> to =.
HoursTV.Ask()
Dim Total1, Prog1
Do
Total1=0.0
Programs.Ask()
' Add up the individual program times
For Each Prog1 in Programs
Total1 = Total1 + Prog1.Item["ProgTime"]
Next
' Compare sum of program times with original total time
If Total1 <> HoursTV Then
NoMatch.Label.Inserts["Total"] = Total1
NoMatch.Label.Inserts["HoursTV"] = HoursTV
NoMatch.Show()
End If
Loop Until (Total1 = HoursTV)
You can also write both these examples using While or For...Next. See Repeat while a condition is true and A set number of repetitions for details.
Jumping out of a Do loop
If you need to jump out of a loop before it has finished executing, insert anExit Do statement at the point you want to leave the loop. When the interviewing program reaches this statement, it skips out of the loop to the statement immediately after Loop.
See also
Repetitive actions in the routing section