Break statement

It is important to define a loop condition when you use the while or foreach statements to implement loops in BI#. This condition determines when the loop is terminated, so that it prevents endless loops. Often the loop condition only checks a counter and terminates the loop when a certain number of iterations have been run. If the loop condition is more complicated and depends on the data that has to be processed, the break statement is useful. This statement terminates a loop immediately.

This example process checks, if an IntArray contains a minimum number of even numbers. It uses the break statement to terminate the loop as soon as the condition is met:

bool CountEvenNumbers(IntArray numbers, int minEvenNumbers)
{
	bool result = false;
	int numberCount = Count(numbers);
	int evenNumbers = 0;
	int i = 0;
	while (i < numberCount)
	{
		if (numbers[i] % 2 == 0)
		{
			evenNumbers = evenNumbers + 1;
			if (evenNumbers >= minEvenNumbers)
			{
				result = true;
				break; // The rest of the numbers does not have to be processed.
			}
		}
		i = i + 1;
	}
	return result;
}