Start a conversation

Using the "for loop" in calculations

In calculations, it is sometimes necessary to repeat a certain action several times. For example, you want to calculate an average of several variables, while some of them are allowed to be empty. In this context, we would need to check each variable, calculate the total sum of the variables that are filled in, and divide it by the number of variables that are filled in. For this purpose, the "for loop" logic is helpful.  

Put in simple terms, the for loop is a reiteration across variables, options, or whatever you have a multitude of. This runs in the form of a loop - so it will go through every defined instance until the condition for ending the loop is met. That means you will have to define where the for loop starts, where it ends and how the iteration is performed (whether on all defined instances).


The syntax of the for loop

for (initialization; condition; final-expression) {
    code block to be executed
}
  • Initialization: this normally sets an initial counter
  • Condition: the loop will run while this condition is met, once the condition is not met, the loop will stop
  • Final-expression: a counter to indicate whether the iterations are performed on all instances
  • Code block to be executed: this code will run every time the iteration is performed 


You will often see something like this in the loop statement:

for (i = 0; i<10; i+1) {
  ...
}

This means that the loop starts at 0 (first instance), ends after 10 iterations and increases by 1 (so the iterations are done through all instances, without skipping). These can be defined differently, depending on what you need to do. This "i" here is useful, because it can be used further within the loop to find the number of the iteration, as in the example below.

The statement in the brackets sets the conditions for the loop, but the actual calculation (what we want the program to do) will be in the code block. The actual calculation is defined as any other and can also contain if/else statements.


For loop example

Let's make it clearer with an example. Here is an example of how to calculate the total score of several variables, while allowing some of these to be left empty.

'##allowempty##';
var variables = [{variable_1}, {variable_2}, {variable_3}]; 
var total = 0; 
for (i=0; i < variables.length; i++) { 
  var value = variables[i]; 
  if (value != 'NA') { 
    total = total + value; 
  } 
};
total;


Let's see what this code means, beginning with the first three lines: 

'##allowempty##';
var variables = [{variable_1}, {variable_2}, {variable_3}];
var total = 0;
  1. This allows the calculation to take into account empty fields, i.e. fields that have not been filled in. 
  2. This line creates a list of your variables of interest. Replace the names in brackets with your own variable names. This list will be used in the for loop. 
  3. A variable is created which will be used to store the total score.


In the next 6 lines, we create a for loop: 

for (i=0; i < variables.length; i++) { 
  var value = variables[i]; 
  if (value != 'NA') { 
    total = total + value; 
  } 
};
  1. The for loop is initiated. A counter "i" is created, which starts at 0. The loop ends when i is equal to the length of the list "variables" defined above is reached. In our case, the loop ends when i = 3, because there are 3 variables in the list. After every loop, i is increased with 1 (i++). 
  2. A variable 'value' is created. It finds each variable in the list, so when i = 0 (the first iteration of the loop), the variable that is first in the list will be found and stored in 'value'.
  3. The if statement checks if the value is not empty ('NA'). If so, the value is added to the total score.


Lastly, the total score is returned:

total;

When you fill in data, you will see that the total score will be shown in the calculation field. If all of the variables were left empty, the total score returned will be 0, because this is what we defined in the beginning.


More information about for loops can be found here: JavaScript For Loop

Choose files or drag and drop files
Was this article helpful?
Yes
No
  1. Castor Support Team

  2. Posted
  3. Updated