Both the For/Do and While/Do structure are designed to ensure that a block of code continues to be executed, over and over again, as long as a defined condition continues to be true. The difference between the two is that a For/Do looping structure repeats based on a value and it is known ahead of time, how many times the loop will repeat while a While/Do structure could end at any time. For additional information on the While/Do structure, refer to the While/Do Structure section of this users manual.
The proper syntax of the For/Do structure is as follows:
for (variable) in (Selection of Records) do
code to be executed
- for (entered in all lower case) indicates the beginning of the structure
- (variable) is a counter that will increment by one each time the loop is repeated
- (Selection of Records) results in a number that indicates exactly how many times the loop should repeat
- do (entered in all lower case) indicates the conclusion of the parameters – name of counter and number of repeats of the loop – section of the structure itself
- code to be executed is the code block to be repeated n number of times where n equals the number of records in the selection (Selection of Records)
- end indicates the ending of the structure
Things to keep in mind when constructing a For/Do structure are:
1. The key elements of the structure, for, do, and end, are case sensitive and must be entered in all lower case
2. the for, do and end elements of the structure are required
3. Variables created inside and structure can only be referenced from inside the structure. Once the end clause is recognized by Ninox, all variables created inside the structure cease to exist. For this reason, and at such time that a variable may be referenced both inside and outside the structure, it should be created and initialized outside the structure.
Below is an example, and analysis, of the for/do structure being used within a Ninox database application.
In the example above, the for statement on line 1 opens the structure and the variable p is defined as the counter. The code on line two indicates that a new record will be created each time the loop repeats and the end statement on line 3 closes the structure itself.
In order to determine how many times the loop will repeat – and how many new records will be created in the ‘Case – Level 1 Tasks’ table – one must look to the Tasks Child Table of the ‘Workflow Template’ Parent Table. As all records in that table make up the selection of records, the number of records in the table is also the number of times the loop will repeat. There are circumstances however when we only want the count of records contained in a subset of all of the records in a table to determine the number of times a loop will repeat. Consider, for example, the following circumstances:
1. A Ninox database consists of two tables, Citizens and Automobile Registrations, where the Citizens table contains one record for every individual who lives in a town and Automobile Registrations contains one record for every one of those citizens who owns as least one automobile.
2. We want to write a block of code that will create a new record in the Automobile Registrations table for every citizen in the town who owns at least one automobile
3. There is a numeric field in the Citizens table that indicates how many automobiles each townsperson owns
Given the set of circumstances defined above, the code block necessary to create the records in the Automobile Registrations table will be as follows:
for c in select(Citizens where ‘Number of Cars’ >0) do
let r := (create ‘Automobile Registrations’)
In the code block above, c is defined as the counter variable and the subset of records consists of all records in the Citizens table where the ‘Number of Cars’ field contains a value greater than 0. The number of records in that subset is equal to the number of times the code block will repeat. It is important to note that in any For/Do loop, the condition under which the loop stops repeating is that the counter variable contains a value equal to the number of records in the Selection of Records.