Tech Tip: Changing the terminating condition of a for loop
PRODUCT: 4D | VERSION: 13.3 | PLATFORM: Mac & Win
Published On: January 24, 2013
After a For Loop is initlized 4D will hold the terminating value to be static. The following loop will run for 10 interations even though the termination conidtion is changed part way through the loop's execution.
For ($i;1;$temp) If ($i=5) $temp:=8 End if End for |
Situations like this often arise when iterating though all of the elements within an array while deleting some of the elements. If any elements are deleted an index out of bounds error will occur.
The solution is to swith the for loop to a while loop. The following while loop is fundamentally equivalent to the earlier for loop.
While ($i<$temp) If ($i=5) $temp:=8 End if $i:=$i+1 End while |