KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Break For Loop
PRODUCT: 4D | VERSION: 19 | PLATFORM: Mac & Win
Published On: September 27, 2021

There is no command to break a For loop in 4D. However, you can break a For loop by setting the counter variable to the end expression.

In the example below, the For loop breaks on the 5th iteration. The counter variable $i is set to the end expression (10) and tells the loop to end. Note that the loop doesn't end right away when the counter is set to the end. It ends only after it reaches the End For. To avoid code running after the 'break statement', include the code to run in the loop in in an If/Else statement so that it only runs when the break condition is not met.

C_TEXT($counter_t)
C_LONGINT($i)
For ($i;1;10)
  
  If ($i=5) //Break Condition
  
     $i:=10 //Break Loop
  
  Else
  
     //Run Code in Loop Here
     //Number of Iterations ($iteration_t contains "1234")
     $iteration_t:=$iteration_t+String($i)
  
  End if
  
End for