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 While/Do looping structure will continue to repeat as long as any condition is true and the condition is not limited to an evaluation of the value of a counter variable compared to the number of records in a subset.
The proper syntax of the For/Do structure is as follows:
while (evaluation of an expression) do
code to be executed
- while (entered in all lower case) indicates the beginning of the structure
- do (entered in all lower case) indicates that the piece of code that is the condition to be evaluated is ended
- (evaluation of an expression) is a line of code that resolves to a value of True or False
- code to be executed is the block of code that is repeated as long as the evaluation expression continues to return a value of True
- end (entered in all lower case) indicates the end of the structure
In a While/Do structure, there must be some line of code that, after being executed a certain number of times, results in the (evaluation of an expression) returning a value of False. Without this, the condition would always be True and the loop would never cease to repeat. An example of a While/Do loop being used in a Ninox database application is detailed below.
Consider the following circumstances:
1. A Ninox accounting database contains two tables – ‘Unposted Transactions’ and ‘Posted Transactions’
2. The database administrator wants to ensure that each batch of unposted transactions that is posted to the other table contains all transactions for a single day
3. At any given point in time, there may be transactions in the unposted table for one day or any number of different days
4. There is a date field in the ‘Unposted Transactions’ table named ‘Transaction Date’. Ninox fills in this field automatically with the current date each time a new transaction is entered
5. There is a field in the ‘Unposted Transactions’ table named ‘Posting Date’ into which a user may enter a single date indicating the date for which all transactions in the unposted table should be moved to the posted table
6. There is a button that appears in Form View of the unposted transactions table that, when clicked, should post all transactions where the transaction date is equal to the posting date that the user has entered
The code assigned to that button would be as follows:
In the code block above, a variable named postDate is initialized and assigned a value equal to the content of the ‘Post Date’ field of the current record in the ‘Unposted Transactions’ table. In line 2, the while/do structure is initiated and will continue to repeat as long as the ‘Transaction Date’ of the current record is equal to the date value assigned to the variable postDate. On line three, the code instructs Ninox to create a new record in the ‘Posted Transactions’ table while the end clause on line 4 indicates the end of the structure.
When the button to which this code is attached is clicked, Ninox is going to evaluate every in the ‘Unposted Transactions’ table. And for each record, Ninox is going to compare the contents of that records ‘Transaction Date’ field to the value assigned to the variable postDate (which is the posting date entered by the end user). For every record where the two dates are identical, the code on line 3 will be executed. And at such time that all the records in the unposted table have been evaluated, the code block will stop running.
You may notice that the while/do loop is actually a combination of the if/then structure and the for/do loop. Consider that same set of 6 circumstances defined above as you look at the code block below.
In the code block above, the postDate variable is created and initialized on line 1 as before. But in this example, the next line – line 2 – contains a for/do loop that is set to repeat once for every record in the ‘Unposted Transactions’ table. And each time that loop repeats, the embedded if/then structure compares the value of the ‘Transaction Date’ field of the current record in ‘Unposted Transactions’ to the contents of the variable postDate. If the result of this comparative evaluation is True, Nionx will go on to execute the code on line 4 which results in the record being posted to the ‘Posted Transactions’ table. If, however, the evaluation returns a value of false, the current record is not posted. This if/then evaluation continues until it has been applied to every record in the ‘Unposted Transactions’ table.
As you can see, each of the code blocks above ends in the same result. The While/Do, in effect, is combining the processing capabilities of the For/Do and the If/Then to deliver that result faster by virtue of the fact that less code must be executed.
As you study the contents of this page, keep the following in mind:
1. The code required to actually post the content of the unposted record to a newly created record in the ‘Posted Transactions’ table has been omitted.
2. To ensure that the same transactions are not posted multiple times to the ‘Posted Transactions’ table, there should be a line of code just after the end of the While/Do loop structure that removes (deletes) all of the records in the ‘Unposted Transactions’ table that were just posted.