Loops – type 2 (repeat while condition is true)

Compatibility: IdSurvey 6 IdSurvey 7
X

By using FlowScript, you can loop a section until a specific answer that determines the end of the loop is selected.

In this example we ask a series of questions about a recent travel, then we ask if other travels were made during the same period. If the answer is yes, the series of questions is repeated for the new travel. The loop stops when the respondent declares no other trips were made.

  1. Ask the respondent if he made any travel during the last year.
  2. Answer some question about the travel, then ask if more travel were made.

As long as the user keeps selecting “yes” when asked about other travels, the answers at point 2 shall be repeated.

 

Step 1

Select the cog button in page P1 and activate FlowScript, then use it to create a variable and assign it value 1.

{i} = 1;

Notes

  • This variable is used as an index (or counter), that will be increased each time the looped section is repeated. Index variables are usually indicated with “i”, “j”, “k” or other single letters. If you use more than an index variable in the same survey, each variable has to be a different name.
  • 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 to group all the pages that needs to be looped.

Step 3

From “Tools” menu, click on “IdCode, Display Questionnaire Script”

Step 4

In the IdCode string where you assign the section code, add the instruction /INDEX "{i}".

Step 5

In the section settings windows (Edit pages -> settings on the selected section), click on the “Logic Conditions” icon and add the following filter:

if(Q1==1)

Notes

  • This instruction is needed to skip the entire section if the respondent declares that he didn’t made any travels during the last year.
  • You can also add this display condition from the global IdCode by adding /F "if(Q1==1)" to the string with the section name.

Step 6

Create a question after those contained in the section, in which the respondent will land after the loop.

  • This question must be added outside the loop and will be recalled with a GoTo in case the answer to Q5 is “no”.

Step 7

In the last page of the section, activate FlowScript from the cog button and write the following instructions:

if (Q5==1) {
{i}++;
goto(Q2);
}
else goto(Q6);

Those strings mean that if question Q5 is answered with code 1 (“yes”), the software increases the index by 1 and goes back to question Q2, else it skips to Q6.

Note

  • This script takes the respondent back to the beginning of the loop (question Q2) as long as he answers “yes” to Q5. If he answers “no”, the “else” instruction gets processed and question Q6 will be displayed instead.

Step 8

Click on the page settings cog button and activate “add header”. In the editor, write a short description of the item to which questions are referring. For example:

Travel N° {i}

Note

  • This step is not mandatory but, thanks to the TextPiping function applied to index {i}, it gives the respondent a visual reference to identify the travel he’s being questioned about, making for an easier experience when filling the survey.

 

IdCode of the entire example:

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

  Q1
  Have you done any travel during the last year?

  1 Yes
  2 No

/section SEC1 /F "if(Q1==1)" /INDEX "{i}"

  /page PQ2
    Travel n. {i}

    Q2
    Please give a brief description of each leg of your travel

    1  ->TM

    Q3
    Have you travelled alone or in a grou?

    1 alone
    2 in a group

  /page PQ4
    Travel n. {i}
    <flowscript>
      if (Q5==1){
              {i}++;
                    goto(Q2);
            }
      else goto (Q6);
    </flowscript>

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

    1 suntanning/beach activity
    2 local lore (local fair, etc.)
    3 sport
    4 healt (spa, hot springs, etc)
    5 local food
    6 cultural tourism (museums, etc.)
    7 shopping
    8 music performances / concert
    9 religious tourism
    10 business
    11 agritourism
    90 other (please specify) ->T

    Q5
    Have you done other travels during the last year?

    1 Yes
    2 No

/endsection

/page PQ6

  Q6
  Respondent's age:

  1 18-24
  2 25-34
  3 35-44
  4 45-54
  5 55-64
  6 65 & over