How To Implement Nested Loops



In many of the surveys there will be a need to repeat a certain question (or group of questions) for many different Topics. For example if you mention certain brands you are aware fo then ask few questions (same ones) on each brand. SurveyToGo enables the easy creation of such scenarios without the need to duplicate the repeating part code in the script.

This is done using loop chapters as explained here.

In some specific scenarios there will be a need to have the repeating parts in more than one level i.e as nested loops. Nested loops are a group of individual loops; each is nested within the other, sharing combined values and logic. SurveyToGo fully supports the implementation of such scenarios as nested loops


Basic terminology/concepts with nested loops


  1. Nested loop (except for the most outer loop chapter / first loop chapter) is a sub-chapter of the loop chapter above it. So for instance, as you can see in the below screen shot, if chapter “Loop 2″ is nested in chapter “Loop 1″, then “Loop 1″ is considered as the “parent” of “Loop 2″:

  2. Each chapter in a nested loops chapters structure has a “level index”, starting from the most inner loop, all the way up to the outer loop. For example, in the below screen shot, from “Loop 3″ point of view, its level is 0, its “parent” loop level (which is “Loop 2″) is 1, and its “grandparent” loop level (which is “Loop 1″) is 2. Yet from “Loop 2″ point of view, its own level is 0, and its “parent” loop level (“Loop 1″) is 1.


 Most common nested loops functions

 It is important to note that each iteration has its own execution order so in a given time during run time we may be at the (for example) 2rd iteration of the outer loop, the 3rditeration of the first inner loop and the 1st iteration of the second inner loop (can be represented as iteration 2-3-1 for example). The following functions refer to referencing the Iteration sequence.

GetParentIterationIndex(int level): Gets a loop chapter’s level and returns its current iteration index.

Example: Loop A is the parent of Loop B. I’m currently inside a question in loop B. The current iterations state is: Loop A is in its second iteration, and loop B is in its fifth iteration.

I want to pipe the text of the selected answer in Q2 that is in loop A, inside the text of the question I’m in currently (in loop B).

So, I’ll put this code in the “loop b” question’s start script:

SetTextFormat(CurrQues,SelectedAnswerTextIter(QRef(2),GetParentIterationIndex(1)); (“Loop A” is level 1 from “Loop B” point of view)


GetParentIterationName(int level): Gets a loop chapter’s level and returns its current iteration name (used exactly as is in GetParentIterationIndex()).


CreateMultiIterationsString(int[] iterations) :Gets a list of iteration indexes from all relevant loop chapters, and creates a indexes string to later use to pull values entered in a specific iterations state.

The list indexes order should be from the most outer loop to the most inner loop.

Example: In my survey I have 3 nested loops, “Loop C” that is nested in “Loop B” that is nested in “Loop A”. “Loop C” has a single choice question, Q10.

After completing the nested loops section, I want to pull the answer that was coded in Q10, while the iterations state was this: “Loop A” was in its fourth iteration, “Loop B” was in its second iteration, and “Loop C” was in its fourth iteration.

So, I’ll pull the value using this code:

AnswerIter(QRef(10), CreateMultiIterationsString(4, 2, 4));


For further information on SurveyToGo visit our support and knowledge portal at

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request



Please sign in to leave a comment.