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
Note You'll notice that each expression uses a different syntax. This is simply to show different ways of achieving the same end result. If you prefer always to use ContainsAny rather than >= or <= (when appropriate) then you may 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
Actions1
Else
Actions2
End If
Parameters
Expression
A logical expression whose result determines which part of the If statement will be executed.
Actions1
One or more statements specifying actions to be carried out if Expression is True.
Actions2
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
Not all conditional actions are based on simple either–or tests; many will 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 Expression1 Then
Actions1
ElseIf Expression2 Then
Actions2
[ElseIf ...
...]
Else
ActionsN
End If
You may 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. It is therefore important that the expressions are mutually exclusive — that is to say, 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 will be always carried out, and the second test will never be reached.
Example
Here is a variation of the first example that combines the three separate If statements into a single group. The question has been changed from multiple choice to single choice to ensure that each respondent satisfies one expression only. The Color question is now:
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.
Note 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 may contain other If clauses. This is called nesting. What this means in practise is that you can accumulate characteristics on an incremental basis, each time reducing the number of respondents eligible to answer the next question. In the following example, we want to ask different questions depending on whether or not the respondent usually travels to work by train. We also want to ask this group of respondents different questions 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 we 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, be careful that you structure the If and Else clauses correctly; otherwise, your script may not do exactly what you expect. Indenting all the statements in each clause by the same amount helps. Make sure that each If has an End If. However, if you find that the logic is becoming difficult to follow, try breaking the block into a number of separate, unnested blocks, and see whether that makes the script easier to work with.
See also