The condition
tag evaluates a logical statement. If the statement is true, the widget is triggered.
start group: MAIN
condition: anyChecked($QDEEPDIVE,1)
12. What do you like about your new car?
type: text
Survey continues here...
end group: MAIN
In this example, the start group
widget includes a condition
tag to determine whether a respondent sees a specific set of questions, as shown above.
Details
- Conditions can be applied directly to groups,
new page
,goto
,set term
,set value
, and more. - Conditions typically are written using checked functions, list functions, and logical operators. They can also include Perl and RegEx.
condition: always
andcondition: never
are also valid inputs. - Logical statements can include an unlimited number of conditions. When combining multiple conditions, always use parentheses to clarify the order of operations.
- To control block content based on a condition, use
%% render if
.
Tip! The condition
decorator works like the tag but is applied directly to list options.
Commonly used functions and methods
Here are some of most common functions and methods used when writing conditional statements.
Function/method | Description |
anyChecked |
The anyChecked function returns "true" if any of the listed response options are selected (or "checked") for the specified question. |
countChecked |
The countChecked function returns the number of items checked in a checkbox question. |
listCount |
The listCount function iterates over a list and evaluates a logical statement for each list iteration. listCount returns the count of the number of iterations which evaluate to "true." |
noneChecked |
The noneChecked function works the opposite of anyChecked . noneChecked returns "true" if none of the response options are checked for the specified question. |
Additional examples
Terminating a respondent with 'set term'
The set term
widget terminates respondents and is useful when termination condition depends on multiple questions.
3. What is your age?
type: radio
1. Under 25
2. 25 - 44
3. 45 or older
4. In what region do you live?
type: radio
1. Northeast
2. South
3. Midwest
4. West
set term: young_NE
condition: anyChecked($Q3,1) and anyChecked($Q4,1)
text: Young people in the Northeast
Sending a respondent out of the survey with 'goto'
goto
routes respondents to a question, group, or page when a condition is true. The survey then continues from that point, following any page or group conditions. In the example below, if a respondent selects option '100. Not real food', they are terminated and flagged with the status "B" for bad.
5. Which of the following have you eaten in the past three months?
type: checkbox
instructions: Select all that apply.
randomize: y
1. Strawberry
2. Banana
3. Apple
4. Pear
5. Sweet potato
6. Carrot
7. Avocado
100. Not real food
99. None of these {exclusive: y} {term: y}
goto: bad
condition: anyChecked($Q5,100)
Coding and skipping a sum-to-100 table
The set value
widget assigns a value to a question or variable. It's typically used to store values for skipped questions.
In the example below, the condition
tag specifies that if a respondent selects only one option at Q3, the selected row is set to '100'. A second set value
ensures the "total" row is also set to '100'. Q4 is then skipped automatically due to having only one answer choice.
3. Select any activities you have tried before.
type: checkbox
1. Running
2. Walking
3. Lifting weights
4. Biking
5. Playing sports
6. Swimming
7. Yoga
set value
question: {'Q4R' . get_first_answer('Q3')}
value: 100
condition: countChecked($Q3) == 1
set value
question: Q4RSCT
value: 100
condition: countChecked($Q3) == 1
4. When you do activities, what percentage of time do you spend doing each one?
type: integer table
range: 0-100
show column total: Total % {total: 100}
postfix: %
rowsfrom: Q3
Multi-part logical conditions utilizing and/or operators
Conditional statements can be thought of like sentences. Like sentences, they can be broken into clauses, with each clause being a complete unit that can be evaluated as either '0' (false) or '1' (true). A statement can consist of a single clause, such as anyChecked($Q5)
, or a statement can consist of multiple clauses, separated by and/or operators.
$Q1 < 5 and $Q5 > 0
anyChecked($Q5) and noneChecked($Q5,1)
$Q1 ne '' or anyChecked($Q1_DTA,1)
Logical statements can include an unlimited number of conditions. When using and/or operators to combine multiple conditions, always use parentheses to clarify the order of operations.
In the example below, either Q1B or Q2B's conditional statement must be true, along with Q3B's condition. If both clauses are true, the respondent will see all the content within the MAIN group.
1B. Do you like to garden?
type: radio
1. Yes
2. No
2B. Have you grown any of the following plants?
type: checkbox
1. Ferns
2. Succulents
3. Herbs
4. Flowers
3B. How many months ago did you last plant something?
type: integer
size: 3
range: <99
postfix: months ago
start group: MAIN
condition: (($Q1B == 1) or (countChecked($Q2B)>1)) and $Q3B <= 6
4B. Topical question
type: text
Survey continues here...
end group: MAIN
Comments
0 comments
Please sign in to leave a comment.