Contact Us

If you still have questions or prefer to get help directly from an agent, please submit a request.
We’ll get back to you as soon as possible.

Please fill out the contact form below and we will reply as soon as possible.

  • Contact Us
  • Home
  • CDMS
  • Castor CDMS Calculations Manual
  • Introduction to calculations: the basics

Using the "for loop" in calculations in EDC/CDMS

Contact Us

If you still have questions or prefer to get help directly from an agent, please submit a request.
We’ll get back to you as soon as possible.

Please fill out the contact form below and we will reply as soon as possible.

  • CDMS
    Castor CDMS Manual Castor CDMS Calculations Manual Frequently Asked Questions Articles for Data Managers Castor CDMS Compliance Release Documents
  • eConsent
    Castor eConsent Manual Castor eConsent Compliance Release Documents
  • SMS
    Castor SMS Manual Castor SMS Compliance Release Documents
  • Castor Connect
    Castor Connect Compliance Release Documents Castor Connect Manual Castor Connect - Participant Quick Start Guide
  • Helpdesk
    News Other Resources Castor products knowledge resources
  • Status page
  • Completing a Study
+ More

Table of Contents

The syntax of the for loop For loop example

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

calculations iteration

Was this article helpful?

Yes
No
Give feedback about this article

Related Articles

  • Using the "if-else" logic in EDC/CDMS
  • Introduction to calculation fields and syntax in EDC/CDMS
  • Forcing Castor to calculate with empty fields in EDC/CDMS
  • Compare several numerical variables against a specific variable in EDC/CDMS
ISO 27001
FDA - 21 CFR part 11
ICH GCP compliant
HIPAA compliant
CDISC
ISO 9001
gdpr compliant

Products & Industries

  • Electronic Data Capture (EDC)
  • ePRO
  • eConsent
  • Decentralized Clinical Trials (DCT)
  • Clinical Data Management
  • Medical Device & Diagnostics
  • Biotech & Pharma
  • CROs
  • Academic Research

Resources

  • Thought Leadership
  • Blog
  • Castor Academy
  • Knowledge Base

 

Company

  • About Us
  • Careers
  • News
  • Contact Support
  • Contact Us

Legal & Compliance

  • Terms of Use
  • Privacy & Cookie Statement
  • Responsible Disclosure Policy
  • Good Clinical Practice (GCP)
  • ISO Compliance Certificates
  • GDPR & HIPAA Compliance
  • Security Statement

© 2022, Castor. All Rights Reserved.

Follow us on social media


Knowledge Base Software powered by Helpjuice

Definition by Author

0
0
Expand