Loops – type 1 (repeat N times)

Compatibility: IdSurvey 6 IdSurvey 7
X

Thanks to FlowScript you can loop a section to repeat N times, based on the answers received in previous questions.

In this example we ask the respondent how many travels did he make during last year and then we ask questions for each travel.

  1. Ask the respondent how many travels he did last year
  2. For each travel, create a few questions.

Questions made at point 2, must be repeated for each travel. For example, if the respondent indicated 3 travels at Q1, then Q2, Q3 and Q4 will be asked four times each.

Step 1

Activate FlowScript from the cogwheel button in page P1, and create a variable and assign value 1 to it.

{i} = 1;

Note

  • This variable is used as index (or counter), that will raise each time the section loops. Those kind of variables are commonly named “i”, “j”, “k” or something like that with a single letter. If the survey uses more than a single index variable, you must name each one differently.
  • You have to set the variable (in this example “i”) before the loop, in the FlowScript of a page that is placed before the section that will loop.

Step 2

Create a section that groups every page that you want to loop.

Step 3

From the “tools” menu, click on “IdCode – Display Questionnaire Script”

Step 4

In the IdCode row in which you assign the section code, add the instruction /INDEX "{i}".

Step 5

In the last page of the section, activate FlowScript from the cogwheel button and add the following code:

if ({i} < [Q1]) {
 {i}++; goto(Q2);
}

This script means that if the index “i” is lower than the number indicated at question Q1, then it will raise the index by 1 and go back to page P2.

Note

  • This script loops to the start of the section until the respondent have given the answers for each of the travels indicated at question Q1.

Step 6

In the section settings window, click on the “Logic” icon, then add the following filter in the Display Condition box.

if({i} <= [Q1])

This filter will show the section if the index value is lower or equal to the number of travels indicated at Q1.

Note

  • This step is not mandatory when creating the loop, though it ensures data coherence in case the respondent, after answering all the questions of the section, may wish to go back to Q1 and lower the travels number. By adding this filter to the section, IdSurvey will automatically wipe the responses from additional answers that would exceed the current number of travels.

Step 7

Click on the page’s settings cogwheel button and activate “Insert Header”.
Then, write in the editor a description of the item to which the questions of the page are referring, for example:

Travel N° {i}

Note

  • This step is optional, but thanks to TextPiping {i} you can show the respondent to which travel are the questions referring, making the survey easier to understand and navigate.

 

IdCode of the entire example

/page P1
  <flowscript>
    {i}=1;
  </flowscript>

  Q1
  How many trips have you done last year?

  1  ->T /V "Ni"

/section SEC1 /F "if ({i}<= [Q1])" /INDEX "{i}"

  /page PQ2
    Travel n°{i}

    Q2
    Briefly describe each step of the travel

    1  ->TM

    Q3
    Have you travelled alone or in a group?

    1 alone
    2 in a group

  /page PQ4
    <flowscript>
      if ({i}< [Q1]){ {i}++;
            goto(Q2); }
    </flowscript>

    Q4 ->M
    What kind of activities have you done during this travel?

    1 beach activities
    2 local culture (i.e. local festivals)
    3 sport
    4 healt and wellness
    5 local food and wine
    6 cultural tourism (i.e. museums)
    7 shopping
    8 concerts
    9 religious activities
    10 business
    11 agritourism
    90 other (specify) ->T

/endsection