C# break Statement

Use the break keyword to stop loops and end a block in a switch statement.
Break. This keyword alters control flow. Its meaning is clear in most programs—it means "stop." In loops it stops the loop. And in a switch it exits the switch.
Break statements are used in certain parts of a program. We use them in for-loops, foreach-loops and switch statements. They cannot be used in some places.ForForeachSwitch
This example shows the break keyword in the for-loop and foreach-loop constructs. A message is printed and then the loop is stopped with break.

Tip: We cannot use the word "break" as a variable identifier—it is reserved. We can prefix it with a @ symbol such as @break.

Break: During execution, the intermediate language corresponding to the break statement is encountered in the program's assembly.

And: Control is immediately transferred to the statement following the enclosing block.

While: We use the for-loop and foreach-loop. The break keyword is often more useful in the while-loop.

C# program that uses break in loops using System; class Program { static void Main() { // Array. int[] array = { 5, 10, 15, 20, 25 }; Console.WriteLine("--- for-loop and break ---"); // Loop through indexes in the array. for (int i = 0; i < array.Length; i++) { Console.WriteLine(array[i]); // ... See if the element has value of 15. if (array[i] == 15) { Console.WriteLine("Value found"); break; } } Console.WriteLine("--- foreach-loop and break ---"); foreach (int value in array) // Use foreach loop { Console.WriteLine(value); // ... See if the iteration variable has value of 15. if (value == 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
Continue, 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.

Tip: If you find too many control statements like break, you may benefit from extracting logic into method and using the returns instead.

Switch. We can use the break keyword in the switch statement. When break is encountered in a switch statement, the switch statement is exited. But the enclosing block is not.

Example: The for-loop continues through all five iterations. Break does not break the loop.

Info: The switch statement can confuse loop code. We might do better to put the switch statement itself in a method with a result value.

Tip: We can employ object-level polymorphism. This calls different methods based on a virtual dispatch table.

C# program that uses loop and switch with break 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 two
No enclosing loop. Sometimes a programmer is unhappy. The "break" keyword, when used outside of a loop, will break the program. It will not compile—an enclosing loop is required here.
C# program that causes break error class Program { static void Main() { break; } } Output Error CS0139 No enclosing loop out of which to break or continue
Yield. The C# language has a yield break statement. Here the compiler generates code that terminates the method from being called again after it returns.

Note: In this sense, the yield break statement is more final than the yield return statement.

Tip: Yield is implemented in the C# compiler—not at the level of the MSIL instructions.

Continue. This stops the current iteration and progresses to the next iteration of the loop. After it is encountered, the loop will execute no more remaining statements in that iteration.Continue

Warning: The continue statement complicates certain loop structures. In loops, as in methods, having one entry and one exit has advantages.

Research. There are many details of how break and loops are compiled. Expert .NET IL Assembler (by Serge Lidin) describes the abstract binary format used by the .NET Framework.

Info: The book contains thorough descriptions of how the "br" instructions are used in the intermediate language.

Research, disasters. The switch statement's traditional usage of the break keyword can cause bugs. Mixing the switch statement in an enclosing loop has caused real-world disasters.

Info: In the book Code Complete, we find a "break" bug made New York City phone systems unusable for 9 hours in January 1990.

Summary. Break stops loop iteration. We tested this keyword on loops. We also looked at the switch statement's somewhat confusing usage of the break keyword.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to
Dot Net Perls