If...Then...Else
If...Then...Else is a flexible way of defining conditional statements. It can be used with expressions of any complexity, based on the answers to one or more questions.
Basic “If” statement
If <expression> Then
<actions>
End If
Parameters
<expression>
A logical expression whose result determines whether the rest of the If statement will be executed.
<actions>
One or more statements specifying actions to be carried out if <expression> is True.
Example
Suppose the metadata section defines the following questions:
AllColors "You had three packets of the test product each with
a different color scheme. Which color schemes do you think
suited the product?" categorical [1..]
{
BluePink "Blue and pink" ,
RedYellow "Red and yellow" ,
YellowGreen "Yellow and green" ,
None "None were suitable" exclusive
};
Red "Why do you think red and yellow is effective?" text [1..];
Blue "Why do you think blue and pink is effective?" text [1..];
Green "Why do you think yellow and green is effective?"
text [1..];
All respondents should be asked the Color question, followed by the questions related to the colors that they say are suitable for the product. So, for example, respondents who think that red and yellow and yellow and green are suitable colors will see the Red and the Green questions, whereas respondents who say that none of the colors is suitable will see no extra questions at all. You specify this in the routing section as follows:
AllColors.Ask()
' Respondents mentioning red and yellow packaging with any others
If AllColors.ContainsAny({RedYellow}) Then
Red.Ask()
End If
' Respondents mentioning blue and pink packaging with any others
If AllColors >= {BluePink} Then
Blue.Ask()
End If
' Respondents mentioning yellow and green packaging with any other
If AllColors <= {YellowGreen} Then
Green.Ask()
End If
Each expression uses a different syntax. This is to show different ways of achieving the same end result. If you prefer always to use ContainsAny rather than >= or <= (when appropriate) then you can do so.
Alternative actions: If...Else...End if statement
If a respondent fails an If test of this type, they continue with the next statement in the routing section. Sometimes, though, you'll want to specify some other action for these respondents. In these cases, you need to include an Else section with the If:
Syntax
If <expression> Then
<actions_1>
Else
<actions_2>
End If
Parameters
<expression>
A logical expression whose result determines which part of the If statement will be executed.
<actions_1>
One or more statements specifying actions to be carried out if <expression> is True.
<actions_2>
One or more statements specifying actions to be carried out if <expression> is False.
Example
In the following example, anyone who mentions red and yellow packaging at the Color question is asked why they think it is a good color for the product. Anyone who does not mention it is asked why they did not choose it:
If AllColors.ContainsAny({RedYellow}) Then
WhyRYGood.Ask()
Else
WhyNotRY.Ask()
End If
Multiple tests
Conditional actions can consist of a number of tests each for different responses. In this situation, you extend the basic If...Then..Else statement to include ElseIf clauses:
If <expression_1> Then
<actions_1>
ElseIf <expression_1> Then
<actions_2>
[ElseIf ...
...]
Else
<actions_n>
End If
You can write as many Else...If clauses as you need. Each respondent passes through the whole If...End If group of statements from top to bottom until one of the expressions returns True. At this point the actions for that expression take place and then the respondent continues with the statement immediately after End If.
The expressions must be mutually exclusive; that is, only one expression must be True for each respondent. If the expressions are not mutually exclusive and a respondent can pass more than one test, the actions for the first test is always carried out, and the second test is never reached.
Example
The following example combines the three If statements into a single group. The question uses single choice to ensure that each respondent satisfies one expression only. The Color question is:
BestColor "Which color scheme do you think suited
the product best?" categorical [1..1]
{
BluePink "Blue and pink" ,
RedYellow "Red and yellow" ,
YellowGreen "Yellow and green" ,
None "None were suitable"
};
The If statement that asks the color-specific questions can be written as follows:
If BestColor={RedYellow} Then
Red.Ask()
ElseIf BestColor = {BluePink} Then
Blue.Ask()
ElseIf BestColor = {YellowGreen} Then
Green.Ask()
End If
Respondents who think that none of the color schemes is suitable fail all three tests and do not see any additional questions.
Test of this type can be specified with less typing if you use a Select Case statement: see
Select Case.
Nested “if” clauses
If clauses can contain other If clauses. This is called nesting. This means that you can accumulate characteristics on an incremental basis, each time reducing the number of respondents eligible to answer the next question. The following example asks different questions depending on whether or not the respondent usually travels to work by train, and according to their general opinion of the train service. The questions are defined in the metadata section as follows. They are listed in the order they are used in the routing section, but this is not a requirement.
Commuter "Are you a daily commuter?" categorical [1..1]
{
Yes, No
};
Transport "How do you usually complete the majority of your journey"
categorical [1..1]
{
Train, Bus, Car, OtherTransport "Other"
};
Ticket "What type of ticket do you buy?" categorical [1..1]
{
Single, Return, Daily "Daily Travelcard",
Weekly "Weekly Season", Monthly "Monthly Season",
Annual "Annual Season",
OtherTicket "Other" other
};
Service "What is your general opinion of the service offered for commuters?"
categorical [1..1]
{
Excellent, VeryGood "Very Good", Satisfactory,
Bad, Appalling
};
WhyBad "Why do you say that?" text [1..100];
Improve "Even though you are generally satisfied with the service offered for
commuters, is there anything about it that you think could be improved?" text [1..100];
EverUseTrain "Do you ever use the train to travel to work?" categorical [1..1]
{
Yes, No
};
The routing statements that implement the questionnaire logic are as follows. The comments explain who sees each question, and the indentation highlights the relationship between the various sections of the code.
Commuter.Ask()
If Commuter = {Yes} Then
Transport.Ask()
' Respondents who travel to work by train ....
If Commuter = {Yes} And Transport = {Train} Then
Ticket.Ask()
Service.Ask()
' ... and who think that the service is bad or appalling
If Service={Bad} Or Service={Appalling} Then
WhyBad.Ask()
' ... or who think that the service is generally good
Else
Improve.Ask()
End If
' Respondents who do not normally travel to work by train
Else
EverUseTrain.Ask()
End If
End If
To illustrate how this example works, suppose you have three people:
▪Person A commutes daily by train and thinks the service is bad. Person A is asked Ticket, Service, and WhyBad.
▪Person B commutes daily by train and thinks the service is very good. Person B is asked ticket, Service, and Improve.
▪Person C commutes but does not use the train. Person C is asked EverUseTrain.
When you write nested loops of this type, structure the If and Else clauses correctly. Indenting all the statements in each clause by the same amount helps. Make sure that each If has an End If. If you find that the logic is difficult to follow, break the block into separate, unnested blocks, to see whether that makes the script easier to work with.
See also