KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Adding a stop condition to a For Each block of code
PRODUCT: 4D | VERSION: 17 | PLATFORM: Mac & Win
Published On: August 17, 2020

One of the things that makes the For Each command rather unique is the ability to add a stop condition to the block of code that is performing the loop via a While condition.

For example, consider the following code that loops over a collection:

For Each($element;$collection)
// do stuff with each $element of the $collection
End For Each


This code can be extended with a while clause that provides a stop condition that will make the loop break upon the condition being true:

For Each($element;$collection) While ($error = 0 )
// do stuff with each $element of the $collection
// loop will stop if $error is set to something other than 0
End For Each


After adding the While condition above, the loop will check this value upon each iteration and the loop will stop once the value no longer matches the condition.