Tech Tip: For each...While vs For each...Until: Iterator Behavior Explained
PRODUCT: 4D | VERSION: 21 | PLATFORM: Mac & Win
Published On: January 7, 2026
When using For each … Until, the loop stops as soon as the condition becomes true.
Unlike For each … While, the iterator does not move to the next element before stopping.
The loop variable keeps the value of the element that caused the stop.
As a result:
Example:
When exiting the loop, $v still contains 30, unlike For each … While, where it would contain 40.
This makes For each … Until preferable when you need to know exactly which element caused the loop to stop.
Unlike For each … While, the iterator does not move to the next element before stopping.
The loop variable keeps the value of the element that caused the stop.
As a result:
- If the condition becomes true in the middle of the collection, the loop stops on that element.
- If the condition becomes true on the last element, the loop also stops on that last element.
Example:
| $col := New collection(10; 20; 30; 40) For each ($v; $col) until ($v=30) //Processing End for each |
When exiting the loop, $v still contains 30, unlike For each … While, where it would contain 40.
This makes For each … Until preferable when you need to know exactly which element caused the loop to stop.