Home
C#
break Statement
Updated Apr 16, 2025
Dot Net Perls
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 loops (like for and while) and switch statements. They cannot be used in some places.
for
while
switch
Example. Here we see the break keyword in the for-loop and foreach-loop constructs. A message is printed and then the loop is stopped with break.
Part 1 We create an array of several integer elements. These are looped over in the 2 loops.
Part 2 We use a for-loop over the indexes in the array, ending at the Length. We test each element for 15, and break if true.
Part 3 We use a foreach-loop, which also supports the break keyword. This loop does the same thing as the for-loop.
foreach
Tip When a break is encountered in the IL, control is immediately transferred to the statement following the enclosing block.
using System; // Part 1: create an array. int[] array = { 5, 10, 15, 20, 25 }; // Part 2: use for-loop and break on value 15. for (int i = 0; i < array.Length; i++) { Console.WriteLine("FOR: {0}", array[i]); if (array[i] == 15) { Console.WriteLine("Value found"); break; } } // Part 3: use foreach-loop and break on value 15. foreach (int value in array) { Console.WriteLine("FOREACH: {0}", value); if (value == 15) { Console.WriteLine("Value found"); break; } }
FOR: 5 FOR: 10 FOR: 15 Value found FOREACH: 5 FOREACH: 10 FOREACH: 15 Value found
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.
Start 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.
abstract
using System; // Loop through five numbers. for (int i = 0; i < 5; i++) { // Use loop index as switch expression. switch (i) { case 0: case 1: case 2: { Console.WriteLine("First three"); break; } case 3: case 4: { Console.WriteLine("Last two"); break; } } }
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.
class Program { static void Main() { break; } }
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.
yield
Continue 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.
Tip If continue and break are confusing, consider extracting logic into methods and using returns instead.
return
Break stops loop iteration, and it is used in switch statements to end a block. The break keyword can be used instead of continue to ensure no more loop iterations run.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Apr 16, 2025 (edit).
Home
Changes
© 2007-2025 Sam Allen