break



Function: break()

The break() function causes the forEach() function to stop immediately and return a _NULL value. $-variables in the environment of the forEach() that were in scope directly before break() was called will still be available after the call to break(). 

If used inside of a forEach() function that is itself nested inside of another forEach() function, break() will only exit the inner (nested) function call. The outer expression will continue to be evaluated in the usual way.

The break() function should only be used within a forEach(). 

Syntax

break()

Examples

In this first example, forEach() is used to iterate over a list of manager names until "Wally" is found, at which point we can stop looking. Used in a string-valued attribute, this would output the value "Found Wally!" into the table record in that field.

Note how $outputString is still available after the call to break() has been made.


do(
   $outputString = "Can't find Wally..."
   
   forEach($manager, $managersList
   	  
	  if($manager = "Wally",
   	     do(
			$outputString "Found Wally!"
			break()
		 )
   	  )
   ),
   
   //output the result
   $outputString
)   

The above example (somewhat contrived, see included() or subset() for a more practical solution) shows how break() can be useful when iterating over lists, if you don't need knowledge of all of the elements.


In this next example, the forEach() function will check each prefix in turn until it finds a prefix length greater than 20 at which point a debug message will be printed out to the log and break() will be called which causes the loop to stop processing.

forEach( $prefix, lookup.prefixes, 

   if( stringLength($prefix) > 20, 
      do ( 
         debug("Stopping because prefix > 20"), 
         break() 
      ) 
   ), 

   $highest = $prefix 
)

See Also