Loops allow programmers to define content that repeats, or "loops," within a survey, with slight variations in each iteration.
Loops offer simpler syntax for repeating questions and are preferred over blocks for efficiency. However, for complex setups (e.g., nested groups with conditions), blocks are still recommended.
set list: CITIES
1. NYC
2. Boston
3. Chicago
4. Los Angeles
5. Atlanta
1. Which of the following activities would you try while in ~text~?
type: checkbox
loop for: CITIES
1. Going to a museum
2. Taking a city tour
3. Visiting a historical site
4. Attending a local sporting event
5. Visiting a brewery or distillery
99. Not interested in any of these {exclusive: y}
Details
- To create a loop, apply the
loop for
tag to astart group
widget and specify the list (or spreadsheet) of elements for which the question(s) will be repeated. Derived lists and question label shorthand are also supported. - All content in the group will repeat for each item in the list unless restricted by condition logic.
-
loop for
can be also applied directly to a question widget for a single-question loop, as shown above. - To reference an option's text, use the
~text~
placeholder; to reference an option's ID, use the~id~
placeholder. These placeholders are not case-sensitive. - To use option data decorators in loops, surround the label with '~'. For example, an option data decorator with the label 'SHORT' would be programmed as '~SHORT~' within a question, option, or row text. These placeholders are not case-sensitive.
-
Condition decorators can be included with the
loop for
tag as needed,(e.g.,loop for: LIST_NAME {if anyChecked($Q1A,[id])}
- Like other listcall tags, the
loop for
tag accepts inclusion/exclusion syntax, (e.g.,loop for: LIST_NAME -[99]
).
Note: ~id~
is not meant for usage with show if
or conditional decorators, but [id]
can be referenced in listcalls.
Referencing loop IDs and iterations
You can reference both option IDs from the list driving a loop, the "looped list," and the iterations within a loop. Here, "id" refers to the loop's option ID, and "iteration" refers to the sequential number of the loop (first, second, etc.).
From within the loop
Code example | Description | Meaning |
10. The store is close to my office {if $_loop_id == 1} |
Allows programmers to specify which option ID from the looped list should be used or shown in a loop. |
Display this answer option for the loop iteration corresponding to option ID 1 in the looped list. |
10. The store is close to my office {if $_loop == 1} |
Allows programmers to specify which loop iteration the option should be used for or shown in the loop. |
Display this answer option for the first iteration of the loop. |
From outside the loop
Additionally, you can reference specific option IDs and loop iterations from outside the loop.
With Perl based syntax
Code example | Description | Meaning |
1. I can go on my lunch break {if anyChecked($Q1__loop__3,10)} |
Allows programmers to reference looped questions and/or specific responses from outside the loop. |
Show this option if at Q1, option 10 was selected, in the loop corresponding with the option ID 3 in the looped list. |
With 'get_answer' and the 'loop' parameter
You can also reference an option ID from the loop list and access previous answers from outside the loop using the get_answer
method and the loop
parameter.
Code example | Description | Meaning |
[* get_answer('QFOO', loop => 2) *] |
Retrieves the response for a question in the specified loop iteration. The loop number corresponds to the option ID from the looped list. For closed-ended questions, it returns the option ID of the response; for open-ended questions, it returns the response content (e.g., text). |
Get the response from question FOO for loop 2, where '2' is the option ID in the looped list. |
[* get_answer('QFOO', loop => 2, text => 1) *] |
For closed-ended questions, retrieves the response text for a question in the specified loop iteration. The loop number corresponds to the option ID from the looped list. This is an alternative to Perl based syntax |
Get the response text from question FOO for loop 2, where '2' is the option ID in the looped list. |
The loop
parameter accepts mathematical and variable inputs as well.
Tags
The following tags are commonly used with groups and can be applied alongside loops.
Tag | Description |
condition |
Evaluates a conditional statement, also known as an "if statement." If the statement is true, the content is displayed; if false, the option content is hidden. |
end group after (alias endafter ) |
Can be applied to the start group widget instead of using an end group widget; specifies the final question to include in the group. |
(alias |
Can be applied to the start group widget instead of using an end group widget; specifies the first question after the group has ended. |
goto |
Routes respondents to a specific question or labeled group/page, then continues the survey from that point, following any conditions set on the page or group. |
inline |
Controls group page break behavior. Use inline: y to place all group content on the same survey page. |
showif |
Evaluates a conditional statement, and specifies when to show questions, tables, or text to the respondent based on a condition being "true." |
skipif |
Similar to showif , evaluates a conditional statement, and specifies when to hide questions, tables, or text to the respondent based on a condition being "true."
|
Additional examples
Placing 'loop for' on a group of questions
When you need to loop through multiple questions, add the loop for
tag to a start group
widget. In the example below, we use ~text~
to "pipe" in (pull in) the city names from the list.
Using option data in loops
Option data can be piped into looped questions just like option text and IDs. Simply specify the option data label as '~OPTION_LABEL~'. In the example below, the 'TYPE' option data placeholder will display either "citrus," "berry," or "melon" depending on what was selected at Q5.
Referencing loop option IDs
Option IDs from the looped list can be referenced within the loop by using $_loop_id == <id>
. In the example below, options in Q7 are conditioned to display only during specific loop iterations based on the respondent's selections in Q6. For example, options assigned '1' in Q7 will be shown when the loop iterates for option 1, 'Action figures'; options assigned '2' in Q7 will be shown for the 'Dolls / stuffed animals' iteration, etc.
Referencing loop iterations
Specific loop iterations can be referenced within the loop using the syntax $_loop == <loop iteration>
. In the example below, the FOOD list loops through Q200, and this list is randomized. Regardless of whether '1. pizza' or '2. pasta' is asked first, this answer option 'The same reason as before' is conditioned to only display the second time the question is asked.
Referencing loop questions and responses within a loop
To reference previous questions within a loop, simply specify the question without mentioning the loop iteration — the system treats loops as self-referential. As shown in Q3C, the autocode
condition and optsfrom
reference are written just as they would be if the question were outside of a loop.
Referencing loop questions and responses outside a loop
Responses from looped questions can be referenced outside of a loop by using the syntax $QX__loop__<loopid>
. For example, in Q10, which focuses on 'Brand 3' (option ID '3' in BRANDLIST), if options 1, 2, or 3 were selected in Q9 for loop 3, Q10 will appear as a follow-up question.
Comments
0 comments
Please sign in to leave a comment.