
The break keyword alters control flow. It has subtle yet important differences depending on its context. Break statements are used in certain parts of a program, including for-loops, foreach-loops and switch-statements.
KeywordsThis C# example program uses the break keyword. Break stops a loop from running.
First, this example shows the reserved break keyword in the for-loop and foreach-loop constructs. Note that you cannot use the word 'break' as a variable identifier as it is reserved, but you can prefix it with a @ symbol such as @break.
IL. When the intermediate language corresponding to the break statement is encountered in the program's assembly, control is immediately transferred to the statement following the enclosing block.
Program that uses break in loops [C#]
using System;
class Program
{
static void Main()
{
int[] array = { 5, 10, 15, 20, 25 }; // Array we loop through.
Console.WriteLine("--- for-loop and break ---");
for (int i = 0; i < array.Length; i++) // Loop through indexes in the array.
{
Console.WriteLine(array[i]);
if (array[i] == 15) // See if the element has value of 15.
{
Console.WriteLine("Value found");
break;
}
}
Console.WriteLine("--- foreach-loop and break ---");
foreach (int value in array) // Use foreach loop
{
Console.WriteLine(value);
if (value == 15) // See if the iteration variable has value of 15.
{
Console.WriteLine("Value found");
break;
}
}
}
}
Output
--- for-loop and break ---
5
10
15
Value found
--- foreach-loop and break ---
5
10
15
Value found
While and do-while loops. The example program above uses the for-loop and foreach-loop constructs, but the break keyword is often more useful in the while-loop. Internally, some of these loops are interchangeable and if you disassemble your program you will often find that there is no difference between for, while, and do-while.
For Loops Foreach Loop ExamplesContinue/return. The C# language also includes the continue and return keywords that transfer instruction control flow. You can think of the 'break' keyword as a local loop return statement. If you find too many control statements like break, you may benefit from extracting logic into method and using the returns instead.

The C# language also allows the break keyword in the switch statement. This unfortunately leads to lots of problems in programs and has for many years through other programming languages. When the keyword break is encountered in a switch statement, the switch statement itself is exited but not the enclosing block. Therefore, in the following example the for-loop continues through all five iterations despite the break keyword being encountered each time.
Program that uses loop and switch with break [C#]
using System;
class Program
{
static void Main()
{
for (int i = 0; i < 5; i++) // Loop through five numbers.
{
switch (i) // Use loop index as switch expression.
{
case 0:
case 1:
case 2:
{
Console.WriteLine("First three");
break;
}
case 3:
case 4:
{
Console.WriteLine("Last two");
break;
}
}
}
}
}
Output
First three
First three
First three
Last two
Last twoBreak conflicts in switch. The example program above shows how the switch statement can confuse your loop code. In some cases, you would do better to put the switch statement itself in a method with a result value. Alternatively, you can employ object-level polymorphism and call different methods based on a virtual dispatch table depending on the value of the switch evaluation—try an abstract class.
Abstract Keyword
The C# programming language also further includes a yield break statement. This is a way to specify that the compiler generate code that terminates the method from being called again after it returns. In this sense, the yield break statement is more final than the yield return statement. The yield keyword and its implementation is not at the level of the MSIL instructions or the CLR itself and is instead implemented in the C# compiler on top of that.
Yield Return ExampleThe continue keyword tells the program's control flow to stop the current iteration but to progress to the next iteration in the loop. This means that after the continue statement is encountered, the loop will not execute the remaining statements in that iteration, but will continue to the next as normal. This complicates certain loop structures.

You can learn the most subtle details of the break keyword and how loops are compiled in the C# language and other .NET languages like VB.NET. The book Expert .NET IL Assembler by Serge Lidin provides the most complete and readable documentation and tutorials for understanding the abstract binary format used by the .NET Framework.
It contains thorough descriptions of how the 'br' instructions are used in the intermediate language. Additionally, the C# Programming Language (3rd Edition) provides an understanding of how the C# language notation expresses the logic at a higher level before it is translated to IL.
Expert .NET 2.0 IL Assembler Review The C# Programming Language: Specification IL
The switch statement's traditional usage of the break keyword can cause bugs in real-world engineering projects. In the book Code Complete by Steve McConnell, it is stated that the New York City phone systems were unusable for nine hours in January 1990 due to a bug in code that used the break keyword incorrectly. Please see Code Complete, 2nd Edition by Steve McConnell page 380.
Code Complete
We looked at examples of using the keyword break in loops using the C# programming language. We tested this keyword on the for-loop, the foreach-loop, and also looked at the switch statement's somewhat confusing usage of the break keyword. Finally, we noted issues related to the break keyword and other places where you encounter it and also pointed out further resources.
Loop Constructs