Professional > Interview scripting > Writing interview scripts > Repetitive actions in the routing section > A set number of repetitions
 
A set number of repetitions
If you want to repeat a set of statements a fixed number of times you can place them inside a For...Next loop.
Syntax
Dim Variable
For Variable = Start To Finish
  Statements
Next
Parameters
Variable
A temporary variable that counts the number of repetitions, and points to each value in the range Start to Finish in turn.
Start and Finish
Define a range that determines the number of times the statements are to be repeated.
Statements
The statements to be repeated.
Example
An example of a For...Next loop is one that repeats a question a set number of times if the respondent has not answered the question correctly. Here is an example. 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;
We want to check that the sum of the times entered at the Programs question equals the total time entered at HoursTV. If the two values do not match, we want to ask the respondent to check their answers and correct them where necessary. We will allow three attempts at getting a correct answer, after which we will set the response to HoursTV to match the sum of the individual times.
HoursTV.Ask()
Dim Total3, Prog3, i
For i = 1 to 3
Total3=0.0
Programs.Ask()
' Add up the individual program times
For Each Prog3 in Programs
Total3 = Total3 + Prog3.Item["ProgTime"]
Next
' Compare sum of program times with original total time
If Total3 = HoursTV Then
Exit For
End If
If Total3 <> HoursTV And i < 3 Then
NoMatch.Label.Inserts["Total"] = Total3
NoMatch.Label.Inserts["HoursTV"] = HoursTV
NoMatch.Show()
Else
HoursTV = Total3
End If
Next
Notice how the script uses Exit For to skip out of the loop as soon as the sum of times matches the total. This takes the respondent to the statement immediately after the Next line. If you wanted to jump to some other location in the routing section, you could use a Goto statement here instead.
When the sum of times does not match the total and the questions have not been asked three times, the script displays an explanatory message and returns to the top of the loop to display the Programs question again. If these times are correct and it is the total that is wrong, the respondent can use the Previous button to return to the HoursTV question and change the answer. Having done this, the respondent then passes through the loop again so that the sum of times can be compared against the new total.
If the respondent is unable to enter a valid set of responses after three attempts, the loop overwrites the respondent’s answer to HoursTV with the sum of the times entered at Programs.
For variations on this scenario that use Do and While loops, see Repeat while or until a condition is true and Repeat while a condition is true
Jumping out of a For loop
If you need to jump out of a loop before it has finished executing, insert anExit For 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 Next. The example earlier in this section shows how this works.
See also
Repetitive actions in the routing section