Loops

Created: - Updated:

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="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. Defines how many columns should be reserved for this loop in the report (i.e. how many times the question will be looped). If this attribute is not defined, the system will try to guess the max loop value, for example by looking at the max selectable value in the piped question. In some cases, for example when the loop count is defined by a variable, the system will not be able to determine max loop automatically.

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.

  • piped loop, iterates as many times as there are piped answers
  • non-piped loop iterates as many times as specified

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 as well, in the following syntax:

<precondition if=" loopcount(F1,0)==0" then="do something the first iteration"/>

<precondition if=" loopcount(F1,0)==1" then="do something else the second iteration"/>

<precondition if=" loopcount(F1,0)==2" then="do something else the third iteration"/>

Loop example

The following is an example of a survey with two loops, where one is nested inside the other. In this example, the outer loop is piped from a "question type multi"-question question with id 1 and the inner loop runs three times.

<survey id="45" wave="1" order="fixed" name="Loop Example" description="" status="live">

  <item id="1"/>

  <folder id="1" name="Fruit loop" alias="F1" order="fixed" pipe_from="#1">

    <item id="2"/>

    <item id="3"/>

    <folder id="2" name="F2" alias="F2" order="fixed" loop="3">

      <item id="4"/>

    </folder>

  </folder>

  <item id="5"/>

</survey>