4D v16.3For...End for |
||
|
4D v16.3
For...End for
For...End for
The formal syntax of the For...End for control flow structure is: For(Counter_Variable;Start_Expression;End_Expression{;Increment_Expression}) The For...End for loop is a loop controlled by a counter variable:
Important: The numeric expressions Start_Expression, End_Expression and Increment_Expression are evaluated once at the beginning of the loop. If these expressions are variables, changing one of these variables within the loop will not affect the loop. Tip: However, for special purposes, you can change the value of the counter variable Counter_Variable within the loop; this will affect the loop.
1. The following example executes 100 iterations: For(vCounter;1;100) 2. The following example goes through all elements of the array anArray: For($vlElem;1;Size of array(anArray)) 3. The following example goes through all the characters of the text vtSomeText: For($vlChar;1;Length(vtSomeText)) 4. The following example goes through the selected records for the table [aTable]: FIRST RECORD([aTable]) Most of the For...End for loops you will write in your databases will look like the ones listed in these examples. In some cases, you may want to have a loop whose counter variable is decreasing rather than increasing. To do so, you must specify Start_Expression greater than End_Expression and a negative Increment_Expression. The following examples do the same thing as the previous examples, but in reverse order: 5. The following example executes 100 iterations: For(vCounter;100;1;-1) 6. The following example goes through all elements of the array anArray: For($vlElem;Size of array(anArray);1;-1) 7. The following example goes through all the characters of the text vtSomeText: For($vlChar;Length(vtSomeText);1;-1) 8. The following example goes through the selected records for the table [aTable]: LAST RECORD([aTable]) If you need to, you can use an Increment_Expression (positive or negative) whose absolute value is greater than one. 9. The following loop addresses only the even elements of the array anArray: For($vlElem;2;Size of array(anArray);2) In some cases, you may want to execute a loop for a specific number of iterations, but then get out of the loop when another condition becomes TRUE. To do so, you can test this condition within the loop and if it becomes TRUE, explicitly set the counter variable to a value that exceeds the end expression. 10. In the following example, a selection of the records is browsed until this is actually done or until the interprocess variable <>vbWeStop, intially set to FALSE, becomes TRUE. This variable is handled by an ON EVENT CALL project method that allows you to interrupt the operation: <>vbWeStop:=False Let's go back to the first For...End for example: The following example executes 100 iterations: For(vCounter;1;100) It is interesting to see how the While...End while loop and Repeat...Until loop would perform the same action. Here is the equivalent While...End while loop: $i :=1 ` Initialize the counter Here is the equivalent Repeat...Until loop: $i :=1 ` Initialize the counter Tip: The For...End for loop is usually faster than the While...End while and Repeat...Until loops, because 4D tests the condition internally for each cycle of the loop and increments the counter. Therefore, use the For...End for loop whenever possible. You can use Real, Integer, and Long Integer variables as well as interprocess, process, and local variable counters. For lengthy repetitive loops, especially in compiled mode, use local Long Integer variables. 11. Here is an example: C_LONGINT($vlCounter) ` use local Long Integer variables You can nest as many control structures as you (reasonably) need. This includes nesting For...End for loops. To avoid mistakes, make sure to use different counter variables for each looping structure. Here are two examples: 12. The following example goes through all the elements of a two-dimensional array: For($vlElem;1;Size of array(anArray)) 13. The following example builds an array of pointers to all the date fields present in the database: ARRAY POINTER($apDateFields;0)
See also
Case of...Else...End case
|
PROPERTIES
Product: 4D
HISTORY
ARTICLE USAGE
4D Language Reference ( 4D v16) |