This article covers these topics:
Introduction to loops
You can loop a group of questions by placing them in a folder and setting certain attributes on the folder. To loop a single question, it must be placed in a folder. Loops can also be nested; placing a looping folder within another looping folder creates a sub-loop. There are two types of loop:
| Loop type | Description |
| Non-piped | A non-piped loop can be set to loop a number of times, specified by the attribute loop. The attribute can also be based on a source variable, e.g. loop="var(myvar)". In this case, the variable is resolved when the loop starts. If the variable value changes during the loop, the number of loop iterations will not change. |
| Piped | A piped loop is based on a multiple-choice source question. The loop will iterate a number of times, equal to the number of selected answers in the source question. Inside a piped loop, the variable answer(Qref) can be used, and it will show the choice text corresponding to the current iteration of the loop. See Using folders in script for examples. |
Loop attributes
Folders have the following attributes to control loops:
|
Attribute |
Description |
| pipe_from | Reference to a multiple-choice question to pipe values from. The loop will run once for every choice selected. |
|
loop |
Loop a specific number of times. |
|
max_loop |
This is an optional attribute. The number entered for max_loop defines how many columns should be reserved for the loop in the project report (i.e. how many times the question(s) is expected to be looped). If your loop is based on a variable (e.g. loop="var(myvar)") and the max_loop attribute has not been defined the system will by default shown columns for three loops, by defining max_loop you can show data from additional loops on the report. Please note max_loop is only defining the amount of columns on the report, the loop will not be ended based on this attribute. If you have a piped loop this attribute is not needed as the system will automatically use the amount of choices on the piped question to determine how many times the question(s) can be looped. |
| loop_while | A condition to control the loop, as long as this condition is true the loop will continue and it will break once the condition evaluates to false. |
Loop count variable
The loop count variable works as a counter, returning the iteration number of the loop. The loop count variable has the following syntax:
loopcount(folder_id,index_from)
index_from defines the number that should be used to represent the first loop iteration; it is usually 0 or 1.
The loop count variable can be used in conditions, in the following syntax:
<precondition if="loopcount(F1,1)==1" then="do something the first iteration"/>
<precondition if="loopcount(F1,1)==2" then="do something the second iteration"/>
<precondition if="loopcount(F1,1)==3" then="do something the third iteration"/>
Example of non-piped loop
The following is an example of the folder configuration for a non-piped loop. The folder is configured with the attribute loop="5" which means the questions inside the folder will be looped 5 times.
<folder id="1" name="Loop Folder" alias="F1" order="fixed" pipe_from="" loop="5">
<item id="3"/>
<item id="4"/>
</folder>
Example of piped loop
The following is an example of the folder configuration of a piped loop. The folder is configured with the attribute pipe_from="Q2". Q2 is a Multiple Choice question that comes before the folder. The questions inside the folder will be looped once for every choice selected at Q2.
<folder id="1" name="Loop Folder" alias="F1" order="fixed" pipe_from="Q2">
<item id="3"/>
item id="4"/>
</folder>
loop_while and max_loop
It is possible to control the amount of loops with for example a variable. In the following example the folder has been configured to loop while var(ExitLoop)==0. max_loop is set to 10 which is the expected maximum amount of loops in this case. Please note max_loop is only defining the amount of columns on the report, the loop will not be ended based on this attribute. On one of the questions inside the folder you will need a condition to change var(ExitLoop) from 0 to something else and that will break the loop.
<folder id="1" name="Loop Folder" alias="F1" order="fixed" loop_while="var(ExitLoop)==0" max_loop="10">
<item id="3"/>
<item id="4"/>
</folder>