The condition
decorator evaluates a conditional statement, also known as an "if statement." If the statement is true, the option is displayed; if false, the option is hidden.
setlist: KPCS
1. Low price
2. Quality
3. Popular brands available
4. Convenient store location { if anyChecked($QC2,1) }
5. Easy to browse app/website { if anyChecked($QC2,2) }
6. Delivery speed { if anyChecked($QC2,2) }
7. Customer reviews and recommendations
8. Reputation of seller
9. Product warranty offered
Note: Although the condition
decorator can be use in a more traditional sense — e.g., {condition: anyChecked($QX,1,2)}
— we recommend the simpler "if statement" as shown above.
Details
- Conditions use an if statement wrapped in curly braces.
- Conditions are most commonly written using checked functions, list functions, and logical operators.
- Conditions can be applied at the list level, where each list element can have a unique condition, or at the question level, where the condition is applied to all options in the list at a listcall.
- Conditions on lists carry forward to all widgets where the list is referenced (questions, tables, etc.), unless overridden by a new condition at the widget level. Adding local conditions at the question level to a listcall will overwrite any list conditions.
- Logic 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.
- Conditions can reference option data stored in lists, for example
{ if ([SHOW]) }
. - Simple conditions can often be replaced with question label shorthand for more concise implementation.
Tip! The condition
tag works like the decorator but is applied directly to blocks, groups, and newpage
widgets without including "if" — for example, condition: anyChecked($QGENDER,1)
.
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
Adding a condition at the question level
In the example below, the anyChecked
condition is placed on the optsfrom
listcall, affecting all items in the BRANDS list. The software replaces the '[id]' placeholder with each option ID from the list. For example, for the first item, the conditional statement anyChecked($Q3R[id],4,5)
becomes anyChecked($Q3R1,4,5)
. If the respondent selected Brand 1 as "Purchased in the past 12 months" or "Purchased in the past 6 months" in table 3, the statement is true, and Brand 1 will appear as an answer choice at Q4.
setlist: BRANDS
1. Brand 1
2. Brand 2
3. Brand 3
3. Which brands have you ever purchased and when?
type: radio table
1. Never purchased
2. Purchased more than 3 years ago
3. Purchased 1 - 3 years ago
4. Purchased in the past 12 months
5. Purchased in the past 6 months
rowsfrom: BRANDS
4. Which brand do you consider your main brand? type: radio optsfrom: BRANDS {if anyChecked($Q3R[id],4,5)}
97. Other {autoother: y}
Overwriting list conditions at the question level
Adding local conditions at the question level to a listcall will overwrite any list conditions. To maintain your list conditions while applying a new one, use the [condition]
option data placeholder as shown at Q3 below.
1. Do you prefer fruits or vegetables? type: radio 1. Fruits 2. Vegetables set list: FRUITVEG 11. Apples { if anyChecked($Q1,1) } 12. Oranges { if anyChecked($Q1,1) } 13. Grapefruit { if anyChecked($Q1,1) } 14. Strawberries { if anyChecked($Q1,1) } 21. Broccoli { if anyChecked($Q1,2) } 22. Cauliflower { if anyChecked($Q1,2) } 23. String beans { if anyChecked($Q1,2) } 24. Carrots { if anyChecked($Q1,2) } 2. Which do you eat most often? type: radio optsfrom: FRUITVEG
3. And which others do you eat sometimes?
type: checkbox
optsfrom: FRUITVEG { if [condition] and noneChecked($Q2,[id]) }
Limiting a list to a subset of options
Often, a list from one question is referenced in a follow-up question, but only with a subset of options based on the respondent's prior answer(s). In other words, the follow-up question dynamically filters the answer set. In the example below, the derived list 'Q101.options' pulls in the full option set used at Q101. Then, the condition
decorator limits the options to be shown to those with an ID less than or equal to the respondent's selection at Q101.
Additionally, a conditional statement is applied to the autocode
tag. This lets respondents skip Q102 if they selected '0' at Q101, while still recording the response for Q102 in the data.
101. How many years have you worked at your company?
type: radio
optsfrom: series [0..19]
20. 20+
102. How many years have you worked in your current position?
type: radio
optsfrom: Q101.options {if [id]<=$Q101}
autocode: $Q101 {if $Q101==0}
Multi-part logical conditions utilizing and/or
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). As previously shown, 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)
Logic 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: text
datatype: whole
size: 3
range: <99
postfix: months ago
startgroup: MAIN
condition: (($Q1B == 1) or (countChecked($Q2B)>1)) and $Q3B <= 6
4B. Topical question
type: text
Survey continues here...
endgroup: MAIN
Comments
0 comments
Please sign in to leave a comment.