Professional > Interview scripting > Writing interview scripts > Repetitive actions in the routing section > Repeat while a condition is true
 
Repeat while a condition is true
The While...End While block is an alternative to Do...While for repeating a set of statements all the time that an expression is True.
Syntax
While Expression
  Statements
End While
Parameters
Expression
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 While 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 Total2, Prog2
Total2=0.0
While (Total2 <> HoursTV)
Total2=0.0
Programs.Ask()
' Add up the individual program times
For Each Prog2 in Programs
Total2 = Total2 + Prog2.Item["ProgTime"]
Next
' Compare sum of program times with original total time
If Total2 <> HoursTV Then
NoMatch.Label.Inserts["Total"] = Total2
NoMatch.Label.Inserts["HoursTV"] = HoursTV
NoMatch.Show()
End If
End While
You can also write this example using Do or For...Next. See Repeat while or until a condition is true and A set number of repetitions for details.
See also
Repetitive actions in the routing section