How to use & implement ‘For’ Loops



‘For’ loops are a very common function\feature which is one of the basic fundamentals of programming.
It used in most of the known programming languages and it is very useful for many different scenarios.

SurveyToGo Studio is open for usage of C# and JavaScript syntax which both have ‘For’ Loops.
This ‘How-To’ will introduce you to ‘For’ loops and will explain how to use them in SurveyToGo.

Step 1: Introduction & Structure of ‘For’ loops.

Step 2: Implementing ‘For’ loops in SurveyToGo.

Let’s go !

Step 1: Introduction & Structure of ‘For’ loops.

For loops are basically iterative loops which get 3 parameters and some code for action, here is how the structure of a ‘For’ loops looks like:

*Green Text are comments*

for( [Initial Expression] ; [condition] ; [Increment Expression] )

{                //Iteration Starts !

    [ACTION];        //Action Code !

}                //Iteration Ends !

[Initial Expression] : This piece of code runs only once , at the moment the loop is entered.

[condition] : This piece of code is the condition which is checked each iteration before it begins.

[Increment Expression] : This piece of code is executed each time an iteration is ended.

Here is an example of how it would really be implemented:

forvar i=; i<10 ; i++ )




* This loop will iterate 10 times, each time it will execute the code written. (notice I used ‘i’ in the code, which means each iteration its value will change !)

1st Param: Declaration of a variable called ‘i’ (this is in JavaScript, in C# ‘var’ should be replaced with ‘int’).

2nd Param: The condition of the loop, as long as ‘i’ value is less than 10, the loop will run.

3rd Param: Here we would usually want to increment by 1 our initialization variable every end of iteration, so the loop will meet it condition sometime and end, if the condition would not be met, the loop will keep on going forever and ever and ever…etc’.
* ‘++’ operator increments the variable from its left by 1.

Action Code (Withing the Curly Bracelets): Here you can write any code you have in mind, your only limit is your own imagination (and syntax limitations J).

Step 2: Implementing ‘For’ loops in SurveyToGo.

For loops can be implemented wherever you want\need to use them, except for entrance rules and jump rules.
You can implement them in Start\End scripts, Survey Init code, Advanced scripts, etc’.

I would show you a nice use of ‘For’ loops in SurveyToGo.
Lets say for the example that we have one multi-topic question (single choice grid) and one
Single-Choice question.
Multi-topic question will have worker names as topics, and grades as answers:

Let’s say, for example, that we want the next single choice question to show only workers which are rated less than average.
*The answers are:
1. Excellent.

In that case we can use a ‘For’ loop that will iterate according the topics and will filter out only the ones suitable to our request, for example.
I would enter in the next question’s start script the following:

for(var i=1; i<=GetTopicCount(CurrQues-1); i++)







basically what I did here is configured a loop to run from 1 until the number of topics in the previous question, incrementing ‘i’ each iteration by 1.

Each iteration I checked if the answer of Topic i, in the previous question, is less or equal to 3.
If the answer is less or equal to 3, what means Average, good or Excellent, so I would like to hide that answer, by using SetAnswerVisible, with the value of ‘i’.
* The single choice question will have the same names in the same order as answers.

Eventually it would hide the workers rated 3 or less, and will show only the workers rated 4 or 5. (Bad, horrible.)

There is no limit to what you can do with ‘For’ loops, this is just a basic example of how you can use it for basic things in SurveyToGo.

That’s it!

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



Please sign in to leave a comment.